如何在中心呈现具有自定义大小的模态视图控制器? [英] How to present a modal view controller with custom size in center?

查看:15
本文介绍了如何在中心呈现具有自定义大小的模态视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想展示一个自定义大小的模态视图控制器,底部有动画.我可以使用 ModalPresentationStyleFormSheet 来实现这个动画,但是 它迫使我使用默认尺寸 540x620 并且我的视图不适合.

I want to present a custom-sized modal view controller with animation from the bottom. I can achieve this animation with ModalPresentationStyle to FormSheet, but it forces me to use the default size which is 540x620 and my view doesn't fit.

如何对放置在屏幕中央的任意大小的视图(控制器)执行类似的转换?

How do I make perform a similar transition to an arbitrarily sized view (controller) placed in center of the screen?

推荐答案

我没有找到从模态控制器本身做到这一点的方法,所以我创建了一个类和一个扩展方法:

I didn't find a way to do it from the modal controller itself so I created a class and an extension method:

public class ModalViewController : UIViewController
{
    public SizeF OriginalViewSize { get; private set; }

    void Initialize ()
    {
        ModalPresentationStyle = UIModalPresentationStyle.FormSheet;
    }

    public override void ViewDidLoad ()
    {
        OriginalViewSize = View.Bounds.Size;
        base.ViewDidLoad ();
    }

    public ModalViewController (IntPtr handle) : base (handle)
    {
        Initialize ();
    }

    public ModalViewController (string nibName, NSBundle bundle) : base (nibName, bundle)
    {
        Initialize ();
    }

    public ModalViewController () : base ()
    {
        Initialize ();
    }
}

public static class ModalViewControllerExtensions
{
    public static void PresentModalViewController (this UIViewController parent, ModalViewController target)
    {
        parent.PresentViewController (target, true, null);

        target.View.Superview.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;
        target.View.Superview.Frame = new RectangleF (PointF.Empty, target.OriginalViewSize);
        target.View.Superview.Center = UIScreen.MainScreen.Bounds.Center ().Rotate ();
    }
}

我大致是这样使用的:

this.PresentModalViewController (
    new PublishModalViewController (Item, HandlePublishAction)
);

我不需要明确指定大小很方便,因为它使用了界面构建器中的根视图边界.我不确定这对自动旋转有何反应,它可能需要一些调整.我还在这里使用了两种扩展方法:

It is convenient that I don't need to specify the size explicitly because it uses root View's bounds from the interface builder. I'm not sure how this reacts to autorotate, it may need some tuning. I'm also using two extension methods here:

public static PointF Rotate (this PointF pt)
{
    return new PointF (pt.Y, pt.X);
}

public static PointF Center (this RectangleF rect)
{
    return new PointF (
        (rect.Right - rect.Left) / 2.0f,
        (rect.Bottom - rect.Top) / 2.0f
        );
}

就是这样.

这篇关于如何在中心呈现具有自定义大小的模态视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆