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

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

问题描述

我想从底部呈现一个带有动画的自定义大小的模态视图控制器。我可以用 ModalPresentationStyle FormSheet 来实现这个动画,但它强制我使用默认大小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 is roughly how I use it:

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天全站免登陆