single function to dismiss all open view controllers
You can call :
self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)
Should dismiss all view controllers above the root view controller.
Swift: Dismissing all view controllers and then presenting a view controller
You can dismiss all viewcontrollers with below code block. In the completion block you can get the topViewController and you can present new viewController over topViewController. I also wrote down an extension for get the topViewController on the window.
UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: true, completion: { [weak self] in
// Get Top Controller With Extension
let topController = UIApplication.topViewController()
// Pressent New Controller over top controller
let congratsPopup = K.mainStoryBoard.instantiateViewController(withIdentifier: "congratsController") as! CongratsController
topController?.present(congratsPopup, animated: true, completion: nil)
})
Get Top View Controller Extension
extension UIApplication {
class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let navigationController = controller as? UINavigationController {
return topViewController(controller: navigationController.visibleViewController)
}
if let tabController = controller as? UITabBarController {
if let selected = tabController.selectedViewController {
return topViewController(controller: selected)
}
}
if let presented = controller?.presentedViewController {
return topViewController(controller: presented)
}
return controller
}
}
Back to root view controller in swift
You can just set a new RootController like this:
let storyboard = UIStoryboard(name: "sName", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "<YOUR ROOT CONTROLLER>")
self.window?.rootViewController = viewController
If you don't have a window at self.window
you need to instanciate one based on your AppDelegate.
If you're within a NavigationController
you can also use the answer of @Anshul Bhatheja
Dismiss all view controllers below current view controller swift
From VC2
, use setViewControllers
method to push VC3
and to remove the rest,
self.navigationController?.setViewControllers([VC3], animated: true)
Related Topics
How to Display the Default iOS 6 Share Action Sheet with Available Share Options
How to Add 2 Buttons into the Uinavigationbar on the Right Side Without Ib
Simply Mask a Uiview with a Rectangle
iOS Designated Initializers:Using Ns_Designated_Initializer
How to Fix the "Uipopovercontroller Is Deprecated" Warning
How to Add Iphonex Launch Image
What Is the Correct Way to Use Prepareforreuse
Detect "Allow Notifications" Is On/Off for iOS8
How to Fix Failed to Fetch Default Token Error
How to Create Auto Incremented Key in Firebase
How to Change the Colors of a Segment in a Uisegmentedcontrol in iOS 13
When Do App Sources Need to Be Included in Test Targets
iPhone - When to Calculate Heightforrowatindexpath for a Tableview When Each Cell Height Is Dynamic
How to Load Local PDF in Uiwebview in Swift
How to Verify That I am Running on a Given Gcd Queue Without Using Dispatch_Get_Current_Queue()