简体   繁体   English

关闭根视图控制器而不显示嵌套/中间视图控制器

[英]Dismissing to root view controller without showing nested / intermediate view controller

I want to dismiss all intermediate child view controllers and show the root view controller.我想关闭所有中间子视图控制器并显示根视图控制器。

The following piece of code works: self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)以下代码有效: self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)

However, I can briefly see the intermediate view controller flash before it animates to dismiss to the root view controller.但是,我可以简要地看到中间视图控制器在它动画关闭到根视图控制器之前闪烁。 Any way, it just directly animates to the root view controller?无论如何,它只是直接动画到根视图控制器?

You can try to set rootViewController again as follow:您可以尝试再次设置 rootViewController 如下:

let navigationController = UINavigationController(rootViewController: ViewController) 
let appdelegate = UIApplication.shared.delegate as! AppDelegate 
appdelegate.window!.rootViewController = navigationController

You need to call你需要打电话

self.view.window?.rootViewController?.dismiss(animated: false, completion: nil)

animation false if you don't want to see intermediate controllers it it still flash you can hide other presented views or make their alpha 0动画假如果你不想看到中间控制器它仍然闪烁你可以隐藏其他呈现的视图或使它们的 alpha 0

like喜欢

if let first = presentedViewController,
        let second = first.presentedViewController,
            let third = second.presentedViewController {
                second.view.alpha = 0
                first.view.alpha = 0
                    third.dismiss(animated: false)

     }

or set directly to rootViewController或直接设置为 rootViewController

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = YourViewController

or in IOS 13 Scene delegate或在 IOS 13 场景委托中

 let scene = UIApplication.shared.connectedScenes.first
        if let getSceneDelegate : SceneDelegate = (scene?.delegate as? SceneDelegate) {
            getSceneDelegate.window?.rootViewController = YourController
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM