Skip to content

Instantly share code, notes, and snippets.

@cemolcay
Created October 29, 2018 07:19
Show Gist options
  • Save cemolcay/9427ec538cbffe6f8de7afe6abb1ab43 to your computer and use it in GitHub Desktop.
Save cemolcay/9427ec538cbffe6f8de7afe6abb1ab43 to your computer and use it in GitHub Desktop.
Returns the top most child in a view controller.
import UIKit
extension UIViewController {
var topMostChild: UIViewController? {
if let tab = self as? UITabBarController {
return tab.selectedViewController?.topMostChild
} else if let nav = self as? UINavigationController {
return nav.topViewController?.topMostChild
} else if let split = self as? UISplitViewController {
return split.viewControllers.last?.topMostChild
} else if let presented = presentedViewController {
return presented.topMostChild
}
return self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment