How to resize a modalViewController with UIModalPresentationPageSheet

7.1k views Asked by At

I have a modal view controller that I show with UIModalPresentationPageSheet. The problem is that its default size is too large for my content: so I want to resize its frame to adjust accordingly with my content.

Does anyone know of a way/trick to do this?

Thanks.

3

There are 3 answers

1
MaTTP On BEST ANSWER

You can't resize a UIModalPresentationPageSheet because its size is not modifable.

1
aslı On

This works for resizing a view controller that's presented as UIModalPresentationFormSheet. I would give this a try, I'm not sure if it'll work or not:

navController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:navController animated:YES];
//these two lines are if you want to make your view smaller than the standard modal view controller size
navController.view.superview.frame = CGRectMake(0, 0, 200, 200);
navController.view.superview.center = self.view.center;
2
Hrafn On

Actually you can resize the view controller that gets presented with UIModalPresentationPageSheet. To do it you need to create a custom view controller class and add the following to the class:

<!--The header file-->
@interface MyViewController: ViewController{
  //Used to store the bounds of the viewController
  CGRect realBounds;
}

<!--In the .m file-->

//viewDidLoad gets called before viewWillAppear, so we make our changes here
-(void)viewDidLoad{
    //Here you can modify the new frame as you like. Multiply the 
    //values or add/subtract to change the size of the viewController.
    CGRect newFrame = CGRectMake(self.view.frame.origin.x, 
                                 self.view.frame.origin.y,       
                                 self.view.frame.size.width, 
                                 self.view.frame.size.height); 

    [self.view setFrame:newFrame];

    //Now the bounds have changed so we save them to be used later on
    _realBounds = self.view.bounds;

    [super viewDidLoad];
}

//viewWillAppear gets called after viewDidLoad so we use the changes 
//implemented above here
-(void)viewWillAppear:(BOOL)animated{

    //UIModalpresentationPageSheet is the superview and we change 
    //its bounds here to match the UIViewController view's bounds.
    [super viewWillAppear:animated];
    self.view.superview.bounds = realBounds;

}

And then you display this view controller with a UIModalPresentationPageSheet. And that's it. This does work for iOS 5.1.1 and iOS 6 as of the date of this post.