如何使用UIModalPresentationPageSheet调整modalViewController的大小

Fry*_*Fry 1 frame ipad modalviewcontroller uimodalpresentationstyle cgrect

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

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

谢谢

Hra*_*afn 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;

}
Run Code Online (Sandbox Code Playgroud)

然后使用UIModalPresentationPageSheet显示此视图控制器.就是这样.从本帖发布之日起,这适用于iOS 5.1.1和iOS 6.