iqra
iqra

Reputation: 1261

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?

Upvotes: 0

Views: 1554

Answers (2)

Kishan Bhatiya
Kishan Bhatiya

Reputation: 2368

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

Upvotes: 2

Jawad Ali
Jawad Ali

Reputation: 14417

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
        }

Upvotes: 1

Related Questions