Skip to content

Instantly share code, notes, and snippets.

@Hexfire
Created May 23, 2017 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hexfire/cc48e9d77c880bb88ea2d9b13b288e25 to your computer and use it in GitHub Desktop.
Save Hexfire/cc48e9d77c880bb88ea2d9b13b288e25 to your computer and use it in GitHub Desktop.
[Swift 3] Visible UIViewController
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