UIWindow not showing over content in iOS 13

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

    Here's a bit hacky way of holding a strong reference to created UIWindow and releasing it after presented view controller is dismissed and deallocated. Just make sure you don't make reference cycles.

    private final class WindowHoldingViewController: UIViewController {
    
        private var window: UIWindow?
    
        convenience init(window: UIWindow) {
            self.init()
    
            self.window = window
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            view.backgroundColor = UIColor.clear
        }
    
        override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
            let view = DeallocatingView()
            view.onDeinit = { [weak self] in
                self?.window = nil
            }
            viewControllerToPresent.view.addSubview(view)
    
            super.present(viewControllerToPresent, animated: flag, completion: completion)
        }
    
        private final class DeallocatingView: UIView {
    
            var onDeinit: (() -> Void)?
    
            deinit {
                onDeinit?()
            }
        }
    }
    

    Usage:

    let vcToPresent: UIViewController = ...
    let window = UIWindow() // or create via window scene
    ...
    window.rootViewController = WindowHoldingViewController(window: window)
    ...
    window.rootViewController?.present(vcToPresent, animated: animated, completion: completion)
    

提交回复
热议问题