Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to present a modal view controller with custom size in center?

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?

like image 651
Dan Abramov Avatar asked Dec 27 '12 01:12

Dan Abramov


People also ask

How do you present a modal view controller programmatically?

Use presentViewController:animated:completion: instead.) The default modal presentation style is a card. This shows the previous view controller at the top and allows the user to swipe away the presented view controller. This is the same for both programmatically created and storyboard created controllers.

What is view controller Xcode?

A view controller manages a single root view, which may itself contain any number of subviews. User interactions with that view hierarchy are handled by your view controller, which coordinates with other objects of your app as needed.


2 Answers

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
        );
}

And this is it.

like image 52
Dan Abramov Avatar answered Oct 13 '22 01:10

Dan Abramov


An easier way is the following

Present the modal view controller as a form sheet and in the modal view controller add:

public override void ViewWillLayoutSubviews ()
{
    base.ViewWillLayoutSubviews ();
    this.View.Superview.Bounds = new RectangleF (0, 0, 900, 700);
}

It is important to set the desired width and height if you don't want ios to assign the default form sheet size

Update: In ios 8 at least there are some situation when the solution above triggeres an infinte loop. It seems that in some situations (like when having an embedded webview in the modal view and tapping in some html input text) changing the superview bounds triggers layout on the modal view resulting in an infinite loop and freeze of the app. But in ios 8 you can simply set the PrefferedSize property to achieve the same effect.

like image 25
Radu Simionescu Avatar answered Oct 13 '22 00:10

Radu Simionescu