从 UINavigationController 中通过自定义转换关闭 UIViewController 时出现黑屏

Posted

技术标签:

【中文标题】从 UINavigationController 中通过自定义转换关闭 UIViewController 时出现黑屏【英文标题】:Black screen when dismissing a UIViewController with custom transition from within a UINavigationController 【发布时间】:2017-12-25 12:41:17 【问题描述】:

我有一个视图控制器FromViewController,我希望它呈现一个带有自定义转换的ToViewController。我已经为 to 和 from 转换实现了 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】:

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

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

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

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

【讨论】:

以上是关于从 UINavigationController 中通过自定义转换关闭 UIViewController 时出现黑屏的主要内容,如果未能解决你的问题,请参考以下文章

从 UINavigationController 切换到 UISplitviewController

从UINavigationController中控制UINavigationItem内容

从 UINavigationController 设置 UITabBarItem 标题?

如何从 UINavigationController 隐藏 UINavigationBar?

UINavigationController 内的 UIPageController 从顶部偏移?

从 UiviewController 切换到 UiNavigationController