从 UINavigationController 中使用自定义转换关闭 UIViewController 时出现黑屏
Black screen when dismissing a UIViewController with custom transition from within a UINavigationController
我有一个视图控制器 FromViewController
,我希望它显示带有自定义转换的 ToViewController
。我已经为 to- 和 from-transitions 实现了 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
}
我检查了该应用程序,UINavigationController
和 FromViewController
似乎都不在关闭后的视图层次结构中。
我明白了。问题是当我将视图添加到转换上下文容器视图时:
[self.containerView insertSubview:self.fromViewController.view belowSubview:self.toViewController.view];
相反,我需要从导航控制器添加视图:
[self.containerView insertSubview:navigationController.view belowSubview:self.toViewController.view];
我有一个视图控制器 FromViewController
,我希望它显示带有自定义转换的 ToViewController
。我已经为 to- 和 from-transitions 实现了 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
}
我检查了该应用程序,UINavigationController
和 FromViewController
似乎都不在关闭后的视图层次结构中。
我明白了。问题是当我将视图添加到转换上下文容器视图时:
[self.containerView insertSubview:self.fromViewController.view belowSubview:self.toViewController.view];
相反,我需要从导航控制器添加视图:
[self.containerView insertSubview:navigationController.view belowSubview:self.toViewController.view];