closures和呈现一个animation的模态视图控制器

问题是,我不知道如何解散,只有一个过渡animation呈现一个视图控制器。

我的故事板结构是:

在这里输入图像说明 我们可以说A控制器是跟在NavigationController之后的, B是Startup引用, C是TabBar ViewController。 BC都以交叉溶解转换的forms呈现。

当用户login到应用程序(从B )时, C控制器以模式方式呈现,并具有翻转水平转换。 当用户注销(从C )时, B以相同的方式呈现。 在控制器上,根据用户是否login,我执行直接跳转到BC.

我的问题是,如果我不closures以前的视图控制器从BC ,该控制器泄漏。 相反,如果我将它解雇,则在目标控制者( BC )出现之前显示A.

是否可以仅显示翻转水平转换并跨越视图?

我对这个问题的解决scheme是取代当前的rootViewController,支持不同的animation:

static func replaceRootViewController(with:UIViewController, transition:UIViewAnimationOptions, completion:(() -> ())? = nil) { if transition == .transitionCrossDissolve { let overlayView = UIScreen.main.snapshotView(afterScreenUpdates: false) with.view.addSubview(overlayView) UIApplication.shared.keyWindow?.rootViewController = with UIView.animate(withDuration: 0.65, delay: 0, options: transition, animations: { overlayView.alpha = 0 }, completion: { finished in overlayView.removeFromSuperview() if let completion = completion{ completion() } }) } else { _ = with.view UIView.transition(with: UIApplication.shared.keyWindow!, duration: 0.65,options: transition, animations: { UIApplication.shared.keyWindow?.rootViewController = with }){_ in if let completion = completion { completion() } } } } 

这是我用来解决这个问题的解决scheme。 我不知道它如何与故事板集成,因为我不使用这些。

将此方法添加到UIViewController的类别中,然后可以调用以前称为presentViewController:animated:completion任何地方presentViewController:animated:completion 。 结果在新的控制器的一个无缝的animation,当仍然解雇前一个时。

 -(void)presentViewControllerDismissingPrevious:(UIViewController* _Nonnull)controller animated:(BOOL)animated completion:(void (^ __nullable)(void))completion { UIViewController* visibleController = self; { UIViewController* temp; while( ( temp = visibleController.presentedViewController ) != nil ) { visibleController = temp; } } if( visibleController == self ) { // no previous controller to dismiss [self presentViewController:controller animated:animated completion:completion]; } else { // create a temporary snapshot of the visible controller's entire window // and add to the current view controller's window until animation completed UIWindow* visibleWindow = visibleController.view.window; UIView* tempView = [visibleWindow snapshotViewAfterScreenUpdates:NO]; UIView* rootView = self.view.window.subviews[0]; tempView.frame = [rootView convertRect:visibleWindow.bounds fromView:visibleWindow]; [rootView addSubview:tempView]; [self dismissViewControllerAnimated:NO completion:^(){ [self presentViewController:controller animated:animated completion:^(){ [tempView removeFromSuperview]; if( completion ) { completion(); } }]; }]; } }