ios5 - size of modal view controller with storyboard

12,868

Solution 1

Yes. In interface builder select the view controller and in the attribute inspector set Size to freeform (from inferred). Then select the view and set its measurements to whatever you require. An important point to note is uncheck Resize View From NIB in the view controller otherwise the view becomes full screen again.

Not sure about the second question if the background is the only problem can't you just set the colour?

Hope this helps.

Solution 2

You can resize a modally-presented view with a custom UIStoryboardSegue. In the storyboard, set the segue to Custom instead of Modal. In the Segue Class, give it the name of your UIStoryboardSegue override.

To override UIStoryboardSegue, you won't need anything in your .h file. It will appear as something like this:

MyStoryboardSegue.h:

#import <UIKit/UIKit.h>
@interface MyStoryboardSegue : UIStoryboardSegue
@end

In MyStoryboardSegue.m, the code would be:

#import "MyStoryboardSegue.h"

@implementation MyStoryboardSegue


- (void)perform
{
    id sourceController = self.sourceViewController;
    id destinationController = self.destinationViewController;
    UIView *destinationView = [destinationController view];
    CGFloat x, y, w, h;

    [destinationController setModalPresentationStyle:UIModalPresentationFormSheet];
    [destinationController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [sourceController presentViewController:destinationController animated:YES completion:nil];

/* 
You have to present the view first, and then resize it. That's because,
as far as I can tell, when you present your view modally, a new view is created
that covers the screen in a black semi-transparent mask to give the shadow effect,
and a container view is placed in the middle of this view that in turn holds the
view you are presenting. Your view automatically resizes to the size of this
container view, so that's the view you need to resize to make your view controller
appear the size you desire. You must present your modal view first 
because until you do, the container view doesn't exist. The following code
resizes the container view (which is now your modal view's superview) and then
resets the position of the container view to the center of the source view that
is presenting the modal view so everything looks nice and centered.
*/
    x = destinationView.superview.frame.origin.x;
    y = destinationView.superview.frame.origin.y;
    w = 400;  // Your desired modal view width
    h = 400;  // Your desired modal view height
    destinationView.superview.frame = CGRectMake(x, y, w, h);
    destinationView.superview.center = [[sourceController view] center];
}

@end

Solution 3

I'm trying to do the same, and in storyBoard my ModalView is smaller , but in simulator it covers all screen...

I think this cannot be done in iPhone... (I've made a simple test in iPad and the modal view worked).

I'll try a semi-transparent view for iPhone and won't use a Modal View.

Hope this helps.

Share:
12,868
Alessandro
Author by

Alessandro

Updated on June 03, 2022

Comments

  • Alessandro
    Alessandro almost 2 years
    1. is there any way to resize a View Controller that has been presented modally using a storyboard segue?

    2. how do I present another View Controller from this modal view controller with a flip transition?

    If I define it as Style=Modal, Presentation=Default, Transition=Flip Horizontal it just looks weird (background is white).

    Thx!

  • Ricardo
    Ricardo about 12 years
    That doesn't work, at least in my case and I do exactly what you said. Any idea? Thanks.
  • DonnaLea
    DonnaLea almost 12 years
    +1 for mentioning the Resize View From NIB checkbox, I never would have found that otherwise.
  • Guillaume Algis
    Guillaume Algis about 11 years
    God. Why can't I upvote more than once ? Thanks a lot Scott !
  • Mark Amery
    Mark Amery almost 11 years
    Can't get this to work. Just created a new single view app to test this and followed the steps exactly; I give the child VC a height and width of 50px, uncheck Resize View From NIB, and then modally present it upon clicking a button in the initial ViewController (either with a modal segue on the storyboard, or using presentViewController: in an action in code), and the child still takes up the full screen. What are we doing differently? If it's not too big an ask, could you perhaps provide an example project to demonstrate that this works?
  • Mark Amery
    Mark Amery almost 11 years
    I should note in addition to the above that this method fails for me both with and without autolayout enabled.
  • Mark Amery
    Mark Amery almost 11 years
    Downvoting; as far as I can tell, this just plain doesn't work at all, in any circumstances. I'll retract the downvote if anybody can provide steps to reproduce this actually working.
  • Scott Sherwood
    Scott Sherwood almost 11 years
    This answer has clearly worked for a number of people! Down voting just because you can't get it to work, isn't in the spirit of Stack Overflow and is not going to get you the constructive response you are looking for!
  • Mark Amery
    Mark Amery almost 11 years
    @ScottSherwood If you can offer up a demo project showing this working, I'll gladly retract the downvote (and upvote the answer too), but from everything I can tell from testing, this just plain doesn't work. It's at most ten minutes work to create a sample project and upload it; if I'm wrong, and this works, why not do so?
  • Pantelis Proios
    Pantelis Proios almost 10 years
    I have done this in Xcode 5 inside a UINavigationController (if that matters) and it doesn't seem to work unless i am missing something...