如何在iOS 8中呈现半透明(半切)视图控制器

时间:2014-06-11 09:06:04

标签: ios8 presentviewcontroller uimodalpresentationstyle

在iOS 7中,此方法没有问题:

_rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[_rootViewController presentViewController:self animated:NO completion:nil];

但是在iOS 8中它没有做任何事情。如何解决?这是iOS 8的Bug吗?

2 个答案:

答案 0 :(得分:13)

我的答案更简单,代码如下。这适用于iOS8(XCode6 GM种子)。

HogeViewController *vc = [[HogeViewController alloc] init];
vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
[self presentViewController:vc animated:NO completion:nil];

答案 1 :(得分:3)

  

请注意xcode6_beta7需要此解决方法。最新的xcode6   已修复UIModalPresentationOver *样式。所以,我只是将它们分配给myModalViewController.modalPresentationStyle,现在它可以正常工作。

在阅读UIPresentationController helpthis post

之后,最终使其在iOS 8中有效
appDelegate.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
MyModalController *myModalController = [[MyModalController alloc] initWithNibName:@"MyModalController" bundle:nil];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myModalController];
navController.modalPresentationStyle = UIModalPresentationCustom;
navController.transitioningDelegate = myModalController;

[self.navigationController presentViewController:navController animated:YES completion:nil];

您可以使模态视图控制器继承自UIViewControllerTransitioningDelegate

@interface MyModalController : UIViewController <UIViewControllerTransitioningDelegate>

并覆盖presentationControllerForPresentedViewController:...

-(UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
{
    if (presented == self) {
        return [[TransparentPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
    } else {
        return nil;
    }
}

返回继承自UIPresentationController

的TransparentPresentationController实例
@interface TransparentPresentationController : UIPresentationController

并覆盖shouldRemovePresentersView

- (BOOL) shouldRemovePresentersView {
    return NO;
}