UIWindow not showing over content in iOS 13

后端 未结 12 1678
野的像风
野的像风 2020-12-07 17:41

I am upgrading my app to use the new UIScene patterns as defined in iOS 13, however a critical part of the app has stopped working. I have been using a UI

12条回答
  •  佛祖请我去吃肉
    2020-12-07 18:39

    Based on all the proposed solutions, I can offer my own version of the code:

    private var window: UIWindow!
    
    extension UIAlertController {
        func present(animated: Bool, completion: (() -> Void)?) {
            window = UIWindow(frame: UIScreen.main.bounds)
            window.rootViewController = UIViewController()
            window.windowLevel = .alert + 1
            window.makeKeyAndVisible()
            window.rootViewController?.present(self, animated: animated, completion: completion)
        }
    
        open override func viewDidDisappear(_ animated: Bool) {
            super.viewDidDisappear(animated)
            window = nil
        }
    }
    

    How to use:

    // Show message (from any place)
    let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "Button", style: .cancel))
    alert.present(animated: true, completion: nil)
    

提交回复
热议问题