iOS UIView Draw(CGRect矩形)永远不会调用当我设置VerticalOptions =“CenterAndExpand”
问题描述:
我正在Xamarin.IOS自定义控件使用iOS UIView。我在Xamarin.Forms Xaml中使用了这个控件。iOS UIView Draw(CGRect矩形)永远不会调用当我设置VerticalOptions =“CenterAndExpand”
如果我将Grid Horizontal和VerticalOptions设置为“CenterAndExpand”,则UIView中的My Draw()不会被调用。它工作正常,如果我将值设置为'FillAndExpand'。
我的要求是将我的自定义控件放置在屏幕的中心。 有什么办法可以解决这个问题吗?或者有其他方法可以在我的屏幕中心显示我的UIView。
<ScrollView>
<Grid HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
<localControls:CustomUIView />
</Grid></ScrollView>
我需要这个Draw(CGRect矩形)方法来获取屏幕中可用大小的宽度和高度。因此我的抽奖是强制性的。
答
您可以使用AbsoluteLayout让视图到屏幕中心。一个AbsoluteLayout内
视图是使用四个值定位成:
- X - 视图的锚
的y(垂直)位置 - 视图的锚- Y的x(水平)位置
- 宽度 - 视图
- 高度的宽度 - 的视图的高度
这些值中的每一个都可以设置为比例值或绝对值。
值被指定为边界和标志的组合。 LayoutBounds是由四个值组成的矩形:x,y,宽度,高度。
代码在XAML这样的:
<ContentPage.Content>
<AbsoluteLayout>
<local:MyCustomView AbsoluteLayout.LayoutBounds="0.5,0.5,300,200" AbsoluteLayout.LayoutFlags="PositionProportional" BackgroundColor="Red"/>
</AbsoluteLayout>
</ContentPage.Content>
AbsoluteLayout.LayoutBounds="0.5,0.5,300,200" AbsoluteLayout.LayoutFlags="PositionProportional"
指锚是屏幕的中心。和视图的宽度和高度为300和200
它的工作原理是这样的:
顺便说一句,我想你的代码,但它工作正常,在我身边与Visual Studio 15.3.5
与Xamarin .iOS 11.0.0.0
,更新您的问题可能会解决CenterAndExpand
问题。