【问题标题】:How to present a semi-transparent (half-cut) viewcontroller in iOS 8如何在 iOS 8 中呈现半透明(半切)视图控制器
【发布时间】:2014-08-01 06:26:30
【问题描述】:

在 iOS 7 中这种方法没有问题:

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

但在 iOS 8 中它什么也没做。如何解决?是iOS 8的Bug吗?

【问题讨论】:

    标签: ios8 presentviewcontroller uimodalpresentationstyle


    【解决方案1】:

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

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

    【讨论】:

    • 如果你在 vc 视图中旋转你的应用程序会发生什么?在我的应用程序中,当方法 - (BOOL)shouldAutorotate { return NO; }
    • 当心。如果您使用的是展开转场,您可能需要在展开转场处理程序中显式调用dismissViewController 以使视图消失。
    【解决方案2】:

    请注意,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;
    }
    

    【讨论】:

    • 我把这个写成另一种说法:
    • 我在两个场景中写这个: 在 ViewController: - (IBAction)present:(id)sender { //场景二 SecondViewController *controller = [[SecondViewController alloc] init]; [控制器在场];对于第二句:-(void)present { UIViewController *root = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; [自我 setTransitioningDelegate:self.transitionController]; self.modalPresentationStyle= UIModalPresentationCustom; [根 presentViewController:self 动画:YES 完成:nil];但它没有做委托方法。为什么?你的是对的。
    • 我覆盖了 shouldRemovePresentersView 以返回 NO。但我仍然无法访问演示者视图。 viewForKey 返回 nil
    猜你喜欢
    • 1970-01-01
    • 2013-09-24
    • 2013-10-11
    • 2014-09-19
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    相关资源
    最近更新 更多