简体   繁体   English

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

[英]How can I change Status Bar Alpha in iOS 13?

I want to change the Status Bar Alpha in iOS 13. 我想在iOS 13中更改状态栏Alpha。

let statusBarWindow = UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow
statusBarWindow?.alpha = 0.5

When I try this, the app crashes (Thread 1: signal SIGABRT). 当我尝试此操作时,应用程序崩溃(线程1:信号SIGABRT)。

The status bar is rendered by a system process (out of the app process) on iOS 13 so there is no way for an app to change this. 状态栏是由iOS 13上的系统进程(不在应用程序进程中)呈现的,因此应用程序无法更改此状态。 (Source: speaking to UIKit engineers at the WWDC 2019 labs.) (来源:在WWDC 2019实验室与UIKit工程师交谈。)

You can see this in the Apple Books app, which used to dim the status bar in its dark mode on iOS 12, but this does not happen on iOS 13. 您可以在Apple Books应用程序中看到此内容,该应用程序过去曾在iOS 12上以暗模式将状态栏变暗,但在iOS 13上则不会发生。

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. 由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“在UIApplication上名为-statusBar或-statusBarWindow的应用程序:此代码必须更改,因为不再有状态栏或状态栏窗口。 Use the statusBarManager object on the window scene instead.' 而是在窗口场景上使用statusBarManager对象。”

You can refer to the following answer to resolve your crash. 您可以参考以下答案来解决崩溃问题。

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
    }

These mentioned solutions worked for me. 这些提到的解决方案为我工作。

I have a custom solution for changing status bar on iOS 13 and below. 我有一个自定义解决方案,用于更改iOS 13及更低版本上的状态栏。 Here is how to do that: 这样做的方法如下:

if #available(iOS 13.0, *) {
   let app = UIApplication.shared
   let statusBarHeight: CGFloat = app.statusBarFrame.size.height

   let statusbarView = UIView()
   statusbarView.backgroundColor = UIColor.red
   view.addSubview(statusbarView)

   statusbarView.translatesAutoresizingMaskIntoConstraints = false
   statusbarView.heightAnchor
     .constraint(equalToConstant: statusBarHeight).isActive = true
   statusbarView.widthAnchor
     .constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
   statusbarView.topAnchor
     .constraint(equalTo: view.topAnchor).isActive = true
   statusbarView.centerXAnchor
     .constraint(equalTo: view.centerXAnchor).isActive = true

} else {
      let statusBar = UIApplication.shared.value(forKeyPath: 
   "statusBarWindow.statusBar") as? UIView
      statusBar?.backgroundColor = UIColor.red
}

Gist 要旨

Also, check the article iOS 13 How to Change StatusBar Color? 另外,请查看文章iOS 13如何更改StatusBar颜色?

One last thing, you can still change statusbar style with : 最后一件事,您仍然可以使用以下命令更改状态栏样式:

 override var preferredStatusBarStyle : UIStatusBarStyle {
    return UIStatusBarStyle.lightContent
    //return UIStatusBarStyle.default   // Make dark again
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM