Created
September 2, 2016 15:45
-
-
Save maxcampolo/329bce4e8c59ac24473da9a8234671f0 to your computer and use it in GitHub Desktop.
Finds the visible view controller in a window heirarchy
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
public extension UIWindow { | |
public var visibleViewController: UIViewController? { | |
return UIWindow.getVisibleViewControllerFrom(self.rootViewController) | |
} | |
public static func getVisibleViewControllerFrom(vc: UIViewController?) -> UIViewController? { | |
if let nc = vc as? UINavigationController { | |
return UIWindow.getVisibleViewControllerFrom(nc.visibleViewController) | |
} else if let tc = vc as? UITabBarController { | |
return UIWindow.getVisibleViewControllerFrom(tc.selectedViewController) | |
} else { | |
if let pvc = vc?.presentedViewController { | |
return UIWindow.getVisibleViewControllerFrom(pvc) | |
} else { | |
return vc | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment