Created
April 16, 2019 12:36
-
-
Save savasadar/fd5354080c49138e6bba5e14dd0cdddd to your computer and use it in GitHub Desktop.
Get the topmost viewController anywhere in the app (typically from the appDelegate). 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() | |
} | |
} | |
//Use Like This | |
//let topMostViewController = UIApplication.shared.topMostViewController() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment