How to resize a modalViewController with UIModalPresentationPageSheet

℡╲_俬逩灬. 提交于 2019-12-04 21:35:05

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

Hrafn

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.

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!