如何在iOS 8中显示一个半透明(半截)的视图控制器

7

iOS 7系统中,此方法没有任何问题:

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

但在 iOS 8 上它没有起作用。如何解决?这是 iOS 8 的一个 Bug 吗?

2个回答

13

我的回答更简单,下面是代码。这个在iOS8(XCode6 GM seed)中有效。

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

1
如果您在VC视图中旋转应用程序,会发生什么情况?在我的应用程序中,当方法-(BOOL)shouldAutorotate返回NO时,它会旋转。 - SuperHappy
注意。如果您正在使用取消展开(segue),则可能需要在取消展开(segue)处理程序中显式调用dismissViewController以使视图消失。 - jlukanta

3
注意,在xcode6_beta7上需要使用此解决方法。最新的xcode6已经修复了UIModalPresentationOver*样式。因此,我只需将它们分配给myModalViewController.modalPresentationStyle,现在它可以正常工作了。
在阅读UIPresentationController帮助这篇文章后,终于使其在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

并且覆盖应该删除演示文稿视图

- (BOOL) shouldRemovePresentersView {
    return NO;
}

我用另一种方式来表达这个意思: - SuperHappy
我将其分为两个场景:在ViewController中:
  • (IBAction)present:(id)sender { //第二个场景 SecondViewController *controller = [[SecondViewController alloc] init]; [controller present]; } 对于第二个场景:-(void)present { UIViewController *root = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; [self setTransitioningDelegate:self.transitionController]; self.modalPresentationStyle= UIModalPresentationCustom; [root presentViewController:self animated:YES completion:nil];
} 但它没有执行委托方法。为什么?你的是正确的。
- SuperHappy
我重写了 shouldRemovePresentersView 方法并返回 NO。但是我仍然无法访问 presenters 视图。viewForKey 返回 nil。 - Shirish Kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接