How can I get the rootViewController of UINavigationController How can I get the rootViewController of UINavigationController ios ios

How can I get the rootViewController of UINavigationController


You can get the root by,

self.navigationController!.viewControllers.first


Or as an extension:

extension UINavigationController {    var rootViewController : UIViewController? {        return viewControllers.first    }}

And then you use it like this:

if let rootv = navigationController?.rootViewController { }


Or maybe this can be used

self.navigationController.visibleViewController

or

self.navigationController.topViewController