Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Get the current visible viewController.
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
extension UIApplication {
func topMostViewController() -> UIViewController? {
return self.keyWindow?.rootViewController?.topMostViewController()
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.