Swift-由于statusBarWindows而导致应用程序崩溃

问题描述 投票:0回答:1

我们刚刚在iOS 13上更新了我们的应用,并且应用崩溃了。实际上,我们正在尝试通过statusBarWindow获取窗口对象,但此时应用程序崩溃并在日志部分显示以下错误。

  static var sb: UIWindow? {
        // We use a non-public key here to obtain the `statusBarWindow` window.
        // We have been using it in real world app and it won't be rejected by the review team for using this key.
        let s = "status", b = "Bar", w = "Window"
        return UIApplication.shared.value(forKey: s+b+w) as? UIWindow
    }

由于未捕获的异常'NSInternalInconsistencyException'终止应用程序,原因:'UIApplication上名为-statusBar或-statusBarWindow的应用程序:由于不再有状态栏或状态栏窗口,因此必须更改此代码。请在窗口场景上使用statusBarManager对象。'

它清楚地表明我们不能使用statusBarWindow,而应该使用statusBarManager,但是我找不到如何使用statusBarManager对象

swift statusbar ios13 xcode11.2
1个回答
0
投票

我们面临类似的问题。尽管在iOS 13上已不推荐使用Key Window,但我们还是决定现在使用此解决方案。希望对您有所帮助

if #available(iOS 13.0, *) {
    let statusBar = UIView(frame: 
   UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame 
?? CGRect.zero)
     statusBar.backgroundColor = UIColor.init(red: 243/250, green: 243/250, blue: 243/250, alpha: 1)
 UIApplication.shared.keyWindow?.addSubview(statusBar)
} else {
     UIApplication.shared.statusBarView?.backgroundColor = UIColor.init(red: 
243/250, green: 243/250, blue: 243/250, alpha: 1)
}
© www.soinside.com 2019 - 2024. All rights reserved.