如何解析viewController到rootViewController而不在解除进程之间显示任何VC?

时间:2018-03-19 21:41:12

标签: ios swift

假设我有以下风险投资:

RootVC - > VC A - > VC B

我正在使用present方法将视图控制器从RootVC呈现到VC A然后再呈现给VC B.现在我在VC B上,我希望使用

从VC B中解除回RootVC
self.view.window!.rootViewController?.dismiss(animated: true, completion: nil)

它有效,但我仍然看到VC A在解雇过程中出现。然后,我尝试这种方法

self.presentationController?.presentedViewController.dismiss(animated: true, completion: nil)

它也可以解散回根VC,但我仍然看到VC A正在进行中。

我的问题是,有没有办法在解雇过程中不显示VC A?我已经尝试了动画:false但仍然得到相同的结果。谢谢!

2 个答案:

答案 0 :(得分:0)

您需要在modalPresentationStyle中对.custom进行更改。当前可见控制器的视图是透明的时,custom将允许您查看presentingViewController视图。

现在,当您想要返回当前呈现堆栈的根视图时,需要调用方法dismissToRootViewController(animated: completion:)

在此方法的实现中,将允许所有中间呈现视图控制器视图透明,这将允许您将动画从VC c关闭到RootVC

extension UIViewController {
    func dismissToRootViewController(animated: Bool, completion: (() -> Swift.Void)? = nil) {
        var viewController = self.presentingViewController
        while viewController?.presentingViewController != nil {
            viewController?.view.alpha = 0.0
            viewController = viewController?.presentingViewController
        }
        self.dismiss(animated: true) {
            viewController?.dismiss(animated: false, completion: completion)
        }
    }
}

答案 1 :(得分:-1)

你可以尝试使用它:

navigationController?.popToRootViewController(animated: false)