Skip to content

Instantly share code, notes, and snippets.

@maxcampolo
Created September 2, 2016 15:45
Show Gist options
  • Save maxcampolo/329bce4e8c59ac24473da9a8234671f0 to your computer and use it in GitHub Desktop.
Save maxcampolo/329bce4e8c59ac24473da9a8234671f0 to your computer and use it in GitHub Desktop.
Finds the visible view controller in a window heirarchy
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