如何使用UIModalPresentationPageSheet调整modalViewController的大小

时间:2012-01-23 14:14:43

标签: ipad frame modalviewcontroller cgrect uimodalpresentationstyle

我有一个模态视图控制器,我用UIModalPresentationPageSheet显示。问题是他的默认大小对我的内容来说太大了:所以我想调整大小是根据我的内容进行调整。

有没有人知道这样做的方法/技巧?

由于

3 个答案:

答案 0 :(得分:6)

实际上,您可以调整使用UIModalPresentationPageSheet呈现的视图控制器的大小。要做到这一点,您需要创建一个自定义视图控制器类并将以下内容添加到类中:

<!--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;

}

然后使用UIModalPresentationPageSheet显示此视图控制器。就是这样。截至本文发布之日,这适用于iOS 5.1.1和iOS 6。

答案 1 :(得分:0)

您无法调整UIModalPresentationFormSheet的大小,因为其大小不可修改。

答案 2 :(得分:-1)

这适用于调整显示为UIModalPresentationFormSheet的视图控制器的大小。我会尝试一下,我不确定它是否会起作用:

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;