从UINavigationController中解除具有自定义转换的UIViewController时出现黑屏

时间:2017-12-25 12:41:18

标签: ios uiviewcontroller uinavigationcontroller presentviewcontroller

我有一个视图控制器FromViewController,我希望它提供一个带有自定义转换的ToViewController。我已经为转换和转换实现了UIViewControllerAnimatedTransitioning,当我出现并关闭ToViewController时,它们都很好用。

但是,如果我的FromViewController包含在UINavigationController中,那么当我关闭ToViewController时,我只会出现黑屏。

这是解雇时UIViewControllerAnimatedTransitioning的代码:

- (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext {
    self.toViewController = (ToViewController *)
    [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UINavigationController *navigationController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    self.fromViewController = (FromViewController *)navigationController.topViewController;
    self.containerView = transitionContext.containerView;

    [self.containerView insertSubview:self.fromViewController.view belowSubview:self.toViewController.view];

    // My animations go here

    // In the animation completion block I call:
    [transitionContext completeTransition:!transitionContext.transitionWasCancelled];  // this is when my screen goes black
}

我检查了应用程序,似乎解雇后UINavigationControllerFromViewController都不在视图层次结构中。

1 个答案:

答案 0 :(得分:2)

我明白了。问题是当我将视图添加到转换上下文容器视图时:

 [self.containerView insertSubview:self.fromViewController.view belowSubview:self.toViewController.view];

我需要从导航控制器添加视图:

[self.containerView insertSubview:navigationController.view belowSubview:self.toViewController.view];