1

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

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

谢谢。

4

3 回答 3

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;

}

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.

于 2012-10-01T11:06:04.703 回答
0

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

于 2012-01-26T09:16:18.293 回答
-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;
于 2012-01-23T14:42:40.657 回答