Skip to content

Instantly share code, notes, and snippets.

@bartleby
Last active July 8, 2018 03:09
Show Gist options
  • Save bartleby/67eeb3f7f4b5f65a6de98a5d8fb5c133 to your computer and use it in GitHub Desktop.
Save bartleby/67eeb3f7f4b5f65a6de98a5d8fb5c133 to your computer and use it in GitHub Desktop.
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