问题:Warning: Attempt to present UINavigationController whose view is not in the window hierarchy

System: Mac OS 10.15.2, XCode 11.3, swift 5.0
Writing Time: 2020-01-06

problem

Warning: Attempt to present UINavigationController whose view is not in the window hierarchy - swift

solve

func topMostController() -> UIViewController {
    var topController: UIViewController = UIApplication.sharedApplication().keyWindow!.rootViewController!
    while (topController.presentedViewController != nil) {
        topController = topController.presentedViewController!
    }
    return topController
}

func demo() {
	let vc = ViewController()
    let nav = UINavigationController.init(rootViewController: vc)
    topMostController().present(nav, animated: true, completion: nil)
}

reference

https://stackoverflow.com/questions/26022756/warning-attempt-to-present-on-whose-view-is-not-in-the-window-hierarchy-s/59608975#59608975

Published 127 original articles · won praise 12 · views 20000 +

Guess you like

Origin blog.csdn.net/zgpeace/article/details/103860075