在iOS 8上调整UIModalPresentationFormSheet的大小

时间:2014-07-18 16:07:37

标签: ios xcode ios8 xcode6

我正在尝试使用UIModalPresentationFormSheet调整在iPad上呈现为模态的UIViewController的大小,虽然我让它在< = iOS 7上运行,但我无法在iOS 8上运行

3 个答案:

答案 0 :(得分:32)

对于iOS 8,您必须设置" preferredContentSize"属性,但对于iOS 7及更早版本,您必须设置superview.frame属性。

示例代码:

UIViewController* modalController = [[UIViewController alloc]init];
modalController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
modalController.modalPresentationStyle =  UIModalPresentationFormSheet;

CGPoint frameSize = CGPointMake([[UIScreen mainScreen] bounds].size.width*0.95f, [[UIScreen mainScreen] bounds].size.height*0.95f);
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

// Resizing for iOS 8
modalController.preferredContentSize = CGSizeMake(frameSize.x, frameSize.y); 
// Resizing for <= iOS 7
modalController.view.superview.frame = CGRectMake((screenWidth - frameSize.x)/2, (screenHeight - frameSize.y)/2, frameSize.x, frameSize.y);

UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[vc presentViewController:modalController animated:YES completion:nil];

答案 1 :(得分:9)

这取决于你是否正在使用UIPopoverPresentationController或UIPresentationController。您可以尝试设置&#34; preferredContentSize&#34;属性是您的新内容大小(&#34; contentSizeForViewInPopover&#34;属性在iOS 8中已弃用)。

答案 2 :(得分:8)

要调整UIViewController视图的大小,当它已显示UIModalPresentationFormSheet模式演示样式并且可见时,请使用iOS 8 +:

self.preferredContentSize = CGSizeMake(width, height);

[UIView animateWithDuration:0.25 animations:^{
    [self.presentationController.containerView setNeedsLayout];
    [self.presentationController.containerView layoutIfNeeded];
}];