模态视图弹出框:总是全屏显示吗?

时间:2018-06-20 02:31:24

标签: xamarin.ios

我已经创建了一个视图。并从底部显示高度200

我正在使用以下代码来展示它(在触摸事件中):

 var pvc = Storyboard.InstantiateViewController("demoStoryboard") as ModalViewController;

            pvc.View.Frame = new CGRect(0, this.View.Frame.Height - 200, this.View.Frame.Width, 200);
            pvc.ModalPresentationStyle = UIModalPresentationStyle.Custom;
            pvc.ModalTransitionStyle = UIModalTransitionStyle.CoverVertical;

            this.PresentViewController(pvc, true, null);

但是,当它显示时,它总是充满屏幕,而不是只有200的高度。

我尝试更改ModalPresentationStyle属性,但没有区别。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

即使将 ModalViewController View Frame 设置为

pvc.View.Frame = new CGRect(0, this.View.Frame.Height - 200, this.View.Frame.Width, 200);

ModalViewController 内部,在 ViewWillAppear ViewDidAppear 方法之间, Frame 属性值正在更改。这是因为在 ViewWillAppear ViewDidAppear 方法之间调用了 ViewWillLayoutSubviews 方法。此方法 ViewWillLayoutSubviews 是实际设置 Frame 属性的位置。它为 Frame 属性设置默认值,即全屏尺寸。因此,这是您应该设置自定义 Frame 属性值的方法,以便覆盖默认值。

也就是说,在 ModalViewController 内部,

public override void ViewWillLayoutSubviews()
{
    base.ViewWillLayoutSubviews();
    View.Frame = new CGRect(0, View.Frame.Height - 200, View.Frame.Width, 200);
}

而且您可以避免在父视图控制器中设置 Frame 属性值。也就是说,

pvc.View.Frame = new CGRect(0, this.View.Frame.Height - 200, this.View.Frame.Width, 200);

可以删除。

相关问题