Created
May 23, 2017 03:03
-
-
Save Hexfire/cc48e9d77c880bb88ea2d9b13b288e25 to your computer and use it in GitHub Desktop.
[Swift 3] Visible UIViewController
This file contains hidden or 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 UIApplication { | |
class func visibleViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? { | |
if let nav = base as? UINavigationController { | |
return visibleViewController(nav.visibleViewController) | |
} | |
if let tab = base as? UITabBarController { | |
let moreNavigationController = tab.moreNavigationController | |
if let top = moreNavigationController.topViewController where top.view.window != nil { | |
return visibleViewController(top) | |
} else if let selected = tab.selectedViewController { | |
return visibleViewController(selected) | |
} | |
} | |
if let presented = base?.presentedViewController { | |
return visibleViewController(presented) | |
} | |
return base | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment