在iOS 8上调整UIModalPresentationFormSheet的大小

Alv*_*nco 18 xcode ios xcode6 ios8

我正在尝试使用UIModalPresentationFormSheet调整在iPad上呈现为模态的UIViewController,虽然我在<= iOS 7上工作,但我无法在iOS 8上工作

小智 33

对于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];
Run Code Online (Sandbox Code Playgroud)


kev*_*inl 9

这取决于你是否使用UIPopoverPresentationController或UIPresentationController.您可以尝试将"preferredContentSize"属性设置为新的内容大小(iOS 8中不推荐使用"contentSizeForViewInPopover"属性).


bte*_*pot 8

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

self.preferredContentSize = CGSizeMake(width, height);

[UIView animateWithDuration:0.25 animations:^{
    [self.presentationController.containerView setNeedsLayout];
    [self.presentationController.containerView layoutIfNeeded];
}];
Run Code Online (Sandbox Code Playgroud)