【问题标题】:iOS 13 setting status bar background coloriOS 13 设置状态栏背景颜色
【发布时间】:2020-01-26 13:27:27
【问题描述】:

我以前有以下代码,但升级到 iOS 13 后,我收到以下错误:

UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 94/225, green: 64/255, blue:204/255, alpha: 1.0)

在 UIApplication 上调用 -statusBar 或 -statusBarWindow 的应用程序:必须更改此代码,因为不再有状态栏或状态栏窗口。在窗口场景中使用 statusBarManager 对象。'

现在状态栏的背景颜色怎么设置?

警告消息提到使用 statusBarManager,所以我做了这样的事情,但我无法让它工作。

var statusBar = UIView()

    if #available(iOS 13.0, *) {
        statusBar = UIView(frame: UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
        statusBar.backgroundColor = UIColor.red
        UIApplication.shared.keyWindow?.addSubview(statusBar)
    } else {
        //ios 12 and below
        UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 94/225, green: 64/255, blue:204/255, alpha: 1.0)
    }

【问题讨论】:

标签: swift statusbar ios13


【解决方案1】:

Kuray 刚刚在这里为我提供了解决方案:

https://freakycoder.com/ios-notes-13-how-to-change-status-bar-color-1431c185e845

将以下内容添加到 viewdidload。请前往他的中号帖子,为他鼓掌!

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

        let statusbarView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: statusBarHeight))
        statusbarView.backgroundColor = UIColor.red
        view.addSubview(statusbarView)
    } else {
        let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
        statusBar?.backgroundColor = UIColor.red
    }

【讨论】:

  • 额外的荣誉,因为你指出这进入了 viewDidLoad()
  • statusBarFrame 已弃用
【解决方案2】:

代码适用于 Swift 5+ 和 iOS 13+

请参考答案:https://stackoverflow.com/a/64924164/7610245

在 UIViewController 扩展中添加了“statusBarColorChange()”。

func statusBarColorChange() {

if #available(iOS 13.0, *) {

    let statusBar = UIView(frame: UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
    statusBar.backgroundColor = .appThemeButtonsColor
        statusBar.tag = 100
    UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.addSubview(statusBar)

} else {

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

    }
}

希望会有所帮助:)

【讨论】:

  • 'windows' 在 iOS 15.0 中已弃用:在相关的窗口场景中使用 UIWindowScene.windows
【解决方案3】:

对于 statusBarFrame 已弃用警告,更新了 Daryl Wong 已接受答案的答案:

 let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
 guard let statusBarHeight = window?.windowScene?.statusBarManager?.statusBarFrame.size.height else { return }
 let statusbarView = UIView(frame: CGRect(x: 0,
                                          y: 0,
                                          width: UIScreen.main.bounds.size.width,
                                          height: statusBarHeight))
 statusbarView.backgroundColor = UIColor.red
 view.addSubview(statusbarView)

【讨论】:

    猜你喜欢
    • 2015-06-01
    • 2013-10-11
    • 1970-01-01
    • 2018-04-10
    • 2016-10-16
    • 2019-11-01
    • 2017-01-10
    • 1970-01-01
    相关资源
    最近更新 更多