UIWindow not showing over content in iOS 13

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

    As everyone else mentioned, the issue is that a strong reference to the window is required. So to make sure that this window is removed again after use, I encapsulated everything needed in it's own class..

    Here's a little Swift 5 snippet:

    class DebugCheatSheet {
    
        private var window: UIWindow?
    
        func present() {
            let vc = UIViewController()
            vc.view.backgroundColor = .clear
    
            window = UIWindow(frame: UIScreen.main.bounds)
            window?.rootViewController = vc
            window?.windowLevel = UIWindow.Level.alert + 1
            window?.makeKeyAndVisible()
    
            vc.present(sheet(), animated: true, completion: nil)
        }
    
        private func sheet() -> UIAlertController {
            let alert = UIAlertController.init(title: "Cheatsheet", message: nil, preferredStyle: .actionSheet)
            addAction(title: "Ok", style: .default, to: alert) {
                print("Alright...")
            }
            addAction(title: "Cancel", style: .cancel, to: alert) {
                print("Cancel")
            }
            return alert
        }
    
        private func addAction(title: String?, style: UIAlertAction.Style, to alert: UIAlertController, action: @escaping () -> ()) {
            let action = UIAlertAction.init(title: title, style: style) { [weak self] _ in
                action()
                alert.dismiss(animated: true, completion: nil)
                self?.window = nil
            }
            alert.addAction(action)
        }
    }
    

    And here is how I use it.. It's from the lowest view controller in the whole apps view hierarchy, but could be used from anywhere else also:

    private let cheatSheet = DebugCheatSheet()
    
    override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
        if motion == .motionShake {
            cheatSheet.present()
        }
    }
    

提交回复
热议问题