Skip to content

Instantly share code, notes, and snippets.

@vibze
Created November 9, 2016 17:08
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 vibze/d9df136bc3dca69f0ec7b08f409f2817 to your computer and use it in GitHub Desktop.
Save vibze/d9df136bc3dca69f0ec7b08f409f2817 to your computer and use it in GitHub Desktop.
UIApplication extension for pulling a topViewController
import Foundation
import UIKit
extension UIApplication {
class func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(base: nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(base: selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(base: presented)
}
return base
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment