Last active
July 8, 2018 03:09
-
-
Save bartleby/67eeb3f7f4b5f65a6de98a5d8fb5c133 to your computer and use it in GitHub Desktop.
Get the current visible viewController.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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