15
votes
  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!

3
Note, you almost always want a modalViewController to take the entire screen. This is because the viewController that is "under" the modalViewController is removed once the modalViewController has appeared. Thus, if you were trying to cover half of the screen with your modalViewController with the intention of having the other viewController showing "underneath", this will not work. It will show white. I accomplish this functionality by adding a view as a subview of the parent view.bearMountain
Your comment might be correct in terms of the behaviour of the framework, but certainly what you "want" should be dictated by the UX of your app and what makes best sense for the user, not the underlying interworkings of the UI framework. Your UX may dictate presenting your modal UI inside of a custom dialog, in which case you need to still see the contents behind the dialog. A custom segue and unwind segue can easily take care of this without removing the previous controller's view by adding/removing the dialog sub-views (darkening, dialog content) manually as needed.Marchy
There is a working solution at stackoverflow.com/questions/25250510/…Katedral Pillon

3 Answers

34
votes

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.

5
votes

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
1
votes

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.