如何在iOS 13中更改状态栏Alpha?

Ben*_*ter 4 alpha statusbar ios swift ios13

我想在iOS 13中更改状态栏Alpha。

let statusBarWindow = UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow
statusBarWindow?.alpha = 0.5
Run Code Online (Sandbox Code Playgroud)

当我尝试此操作时,应用程序崩溃(线程1:信号SIGABRT)。

Dou*_*ill 6

状态栏是由iOS 13上的系统进程(不在应用程序进程中)呈现的,因此应用程序无法更改此状态。(来源:在WWDC 2019实验室与UIKit工程师交谈。)

您可以在Apple Books应用程序中看到这一点,该应用程序过去曾在iOS 12上以暗模式将状态栏变暗,但在iOS 13上则不会发生这种情况。


小智 6

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

您可以参考以下答案来解决崩溃问题。

var statusBarUIView: UIView? {
        if #available(iOS 13.0, *) {
            let tag = 38482458
            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
    }
Run Code Online (Sandbox Code Playgroud)

这些提到的解决方案为我工作。