简体   繁体   中英

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)

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:

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

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

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

or in IOS 13 Scene delegate

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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