当我将状态栏的背景色更改为本机UIColor.gray时,它也会更改。
但是,当我要使用自定义颜色时,它会变成黑色。
UIApplication.shared.statusBarView?.backgroundColor = UIColor.gray-此代码正常工作。状态栏背景颜色为灰色
UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 30/255, green: 30/255, blue: 30/255, alpha: 1)-此代码无法正常工作。状态栏背景颜色为黑色

最佳答案

首先,在info.plist文件中设置基于View控制器的状态栏外观属性No。
然后在AppDelegate类的didFinishLaunchingWithOptions方法中添加以下代码。

extension UIApplication {
var statusBarView: UIView? {
    if #available(iOS 13.0, *) {
        let tag = 5111
        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}
}
希望对您有帮助。

10-08 14:38