UIWindow not showing over content in iOS 13

后端 未结 12 1679
野的像风
野的像风 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:36

    Swift 4.2 iOS 13 UIAlertController extension

    This code full working in iOS 11, 12 and 13

    import Foundation
    import UIKit
    
    extension UIAlertController{
        private struct AssociatedKeys {
            static var alertWindow = "alertWindow"
        }
        var alertWindow:UIWindow?{
            get{
                guard let alertWindow = objc_getAssociatedObject(self, &AssociatedKeys.alertWindow) as? UIWindow else {
                    return nil
                }
                return alertWindow
            }
            set(value){
                objc_setAssociatedObject(self,&AssociatedKeys.alertWindow,value,objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            }
        }
    
        func show(animated:Bool) {
            self.alertWindow = UIWindow(frame: UIScreen.main.bounds)
            self.alertWindow?.rootViewController = UIViewController()
            self.alertWindow?.windowLevel = UIWindow.Level.alert + 1
            if #available(iOS 13, *){
                let mySceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate
                mySceneDelegate!.window?.rootViewController?.present(self, animated: animated, completion: nil)
            }
            else{
                self.alertWindow?.makeKeyAndVisible()
                self.alertWindow?.rootViewController?.present(self, animated: animated, completion: nil)
            }
        }
    }
    

提交回复
热议问题