Created
January 4, 2020 08:49
-
-
Save monsoir/9cdeaa6a8827d0b5a557ff15924f833b to your computer and use it in GitHub Desktop.
UIViewController extensions
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 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() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment