在 UIApplication 上调用 -statusBar 或 -statusBarWindow 的应用程序

App called -statusBar or -statusBarWindow on UIApplication

我正在尝试使用 Xcode 11 beta 6 和 iOS 13 beta 8 构建我的应用程序,但是一旦它开始 运行:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.'

什么是window场景,如何使用statusBarManager
我不确定这是否相关,但我没有使用任何 SwiftUI.

您需要添加以下扩展程序才能访问状态栏视图:

extension UIApplication {
    var statusBarUIView: UIView? {
        if #available(iOS 13.0, *) {
            let tag = 38482458385
            if let statusBar = self.keyWindow?.viewWithTag(tag) {
                return statusBar
            } else {
                let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
                statusBarView.tag = tag

                self.keyWindow?.addSubview(statusBarView)
                return statusBarView
            }
        } else {
            if responds(to: Selector(("statusBar"))) {
                return value(forKey: "statusBar") as? UIView
            }
        }
        return nil
    }
}

要在 Swift 5+ 和 iOS 13+

中访问 StatusBar

需要将 -satusBar 或 -statusWindow 替换为 statusBarManager

if #available(iOS 13.0, *) {

        let statusBar = UIView(frame: UIApplication.shared.windows.filter {[=10=].isKeyWindow}.first?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
        statusBar.backgroundColor = .appNavigationThemeColor
          //  statusBar.tag = 100
        UIApplication.shared.windows.filter {[=10=].isKeyWindow}.first?.addSubview(statusBar)

    } else {

            let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
            statusBar?.backgroundColor = .appNavigationThemeColor

        }
    }

希望能有所帮助:)