Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get the top most viewController anywhere in the app (typically from the appDelegate). Get the current visible viewController.
extension UIViewController {
func topMostViewController() -> UIViewController {
if self.presentedViewController == nil {
return self
}
if let navigation = self.presentedViewController as? UINavigationController {
return navigation.visibleViewController.topMostViewController()
}
if let tab = self.presentedViewController as? UITabBarController {
if let selectedTab = tab.selectedViewController {
return selectedTab.topMostViewController()
}
return tab.topMostViewController()
}
return self.presentedViewController!.topMostViewController()
}
}
extension UIApplication {
func topMostViewController() -> UIViewController? {
return self.keyWindow?.rootViewController?.topMostViewController()
}
}
@ntoonio

This comment has been minimized.

Copy link

commented Jan 17, 2018

Thank you! Just a note if anyone else uses this. If your root view is either a tab or navigation view as it was in my case this will not work.

I added this code above if self.presentedViewController == nil {

if let navigation = self as? UINavigationController {
    return navigation.visibleViewController!.topMostViewController()
}
	
if let tab = self as? UITabBarController {
    if let selectedTab = tab.selectedViewController {
        return selectedTab.topMostViewController()
    }
    return tab.topMostViewController()
}
@dawand

This comment has been minimized.

Copy link

commented Jan 24, 2018

Thank you for this gist. Just a small note that visibleViewController is optional so it would be safer to do this:

 if let navigation = self.presentedViewController as? UINavigationController {
        if let visibleController = navigation.visibleViewController {
             return visibleController.topMostViewController()
        }
}
@maryamaffinityclick

This comment has been minimized.

Copy link

commented May 17, 2018

above one would not work cause you have to return something, you can use this instead on line 7 :
return navigation.visibleViewController?.topMostViewController() ?? navigation

@bartleby

This comment has been minimized.

Copy link

commented Jul 8, 2018

extension UIViewController {
    func topMostViewController() -> UIViewController {
        
        if let presented = self.presentedViewController {
            return presented.topMostViewController()
        }
        
        if let navigation = self as? UINavigationController {
            return navigation.visibleViewController?.topMostViewController() ?? navigation
        }
        
        if let tab = self as? UITabBarController {
            return tab.selectedViewController?.topMostViewController() ?? tab
        }
        
        return self
    }
}

@OkiRules

This comment has been minimized.

Copy link

commented Jul 10, 2018

This is how to use this code:

let topMostViewController = UIApplication.shared.topMostViewController()

@bj97301

This comment has been minimized.

Copy link

commented Aug 28, 2018

Thanks bud

@anaidfazlic

This comment has been minimized.

Copy link

commented Nov 8, 2018

extension UIViewController {
    func topMostViewController() -> UIViewController {
        
        if let presented = self.presentedViewController {
            return presented.topMostViewController()
        }
        
        if let navigation = self as? UINavigationController {
            return navigation.visibleViewController?.topMostViewController() ?? navigation
        }
        
        if let tab = self as? UITabBarController {
            return tab.selectedViewController?.topMostViewController() ?? tab
        }
        
        return self
    }
}

@bartleby Thank you sir.

@patelgaurav4u

This comment has been minimized.

Copy link

commented Mar 18, 2019

i think need to add for pageviewcontroller related code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.