1

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.

Crt Gregoric
  • 392
  • 1
  • 5
  • 12
Fry
  • 6,235
  • 8
  • 54
  • 93

3 Answers3

6

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.

Edward Dale
  • 29,597
  • 13
  • 90
  • 129
Hrafn
  • 2,867
  • 3
  • 25
  • 44
  • This answer is awesome. I understand the poster's question because I need this exact same thing. It works. This should be approved answer. – Brandon Brodjeski Oct 15 '12 at 00:10
  • 1
    This answer is a great start, but extremely limited by the fact that there doesn't seem to be a way to get the custom sized formview centered. Centering the various views and superviews doesn't have the desired effect. – memmons Jan 14 '13 at 18:11
0

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

MaTTP
  • 953
  • 2
  • 12
  • 27
  • 1
    Actually you can by using a custom viewcontroller, that gets presented in a UIModalPresentationPageSheet, and referencing its superview and thus changing its size. I have an example below. – Hrafn Oct 17 '12 at 17:46
-1

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;
aslı
  • 8,740
  • 10
  • 59
  • 80
  • not work. In my post I said that I need to use PageSheet with custom frame for some reason that I don't want explain. I can't use FormSheet. – Fry Jan 23 '12 at 15:54