Swift 5
Here is solution that addresses the issue mentioned with previous answers, when isModal()
returns true
if pushed UIViewController
is in a presented UINavigationController
stack.
extension UIViewController {
var isModal: Bool {
if let index = navigationController?.viewControllers.firstIndex(of: self), index > 0 {
return false
} else if presentingViewController != nil {
return true
} else if navigationController?.presentingViewController?.presentedViewController == navigationController {
return true
} else if tabBarController?.presentingViewController is UITabBarController {
return true
} else {
return false
}
}
}
It does work for me so far. If some optimizations, please share.