Skip to content

Instantly share code, notes, and snippets.


Created May 18, 2017
What would you like to do?
the description for this gist
class NavigationRouter {
static func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return self.topViewController(base: nav.visibleViewController)
if let tab = base as? UITabBarController,
let selected = tab.selectedViewController {
return self.topViewController(base: selected)
if let presented = base?.presentedViewController {
return self.topViewController(base: presented)
return base
static func present(_ viewController: UIViewController, animated: Bool = true) {
NavigationRouter.topViewController()?.present(viewController, animated: animated)
static func show(_ viewController: UIViewController) {
NavigationRouter.topViewController()?.show(viewController, sender: nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.