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

系统: Mac OS 10.15.2, XCode 11.3,swift 5.0
写作时间:2020-01-06

问题

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

解决

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)
}

参考

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

发布了127 篇原创文章 · 获赞 12 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/zgpeace/article/details/103860075