更改状态栏背景颜色而无需导航栏(iOS 13)

时间:2019-09-14 16:02:20

标签: ios ios13

我正在寻找一种更改statusBar的背景色的方法。有类似的问题,但是它们都需要一个NavigationBar。我找到了this answer,但这没有任何意义。他使用已弃用的属性。

对于iOS 12及以下版本,更改背景颜色非常简单:

let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
statusBar.backgroundColor = UIColor.black

但是对于iOS 13,它看起来确实很复杂。有没有类似的方法可以更改它?

1 个答案:

答案 0 :(得分:0)

您可以使用以下方式更改状态栏颜色。它将在iOS 13上运行。希望它对您有用

guard let windowScene = (scene as? UIWindowScene) else { return }
if let statusBarFrame = windowScene.statusBarManager?.statusBarFrame {
     let statusBarHeight = statusBarFrame.size.height
     let statusBarView = UIView.init(frame: CGRect.init(x: 0, y: -statusBarHeight, width: UIScreen.main.bounds.width, height: statusBarHeight))
     statusBarView.backgroundColor = UIColor.yellow
     self.navigationController?.navigationBar.addSubview(statusBarView)
}

这里是参考链接 How to change the status bar background color and text color on iOS 7?