首页 > 解决方案 > 在没有导航栏的情况下更改状态栏背景颜色(iOS 13)

问题描述

我正在寻找一种方法来更改状态栏的背景颜色。有类似的问题,但都需要一个导航栏。我找到了这个答案,但这没有任何意义。他使用已弃用的属性。

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

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

但对于 iOS 13,它看起来真的很复杂。没有类似的方法可以改变它吗?

标签: iosios13

解决方案


您可以使用以下方式更改状态栏颜色。它适用于 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?


推荐阅读