在iOS 13中,如何更改状态栏背景颜色而不涉及导航栏?

3
我正在寻找一种更改状态栏背景颜色的方法。虽然有类似的问题,但它们都需要一个navigationBar。我找到了这个答案,但它没有意义。他使用已废弃的属性。
对于iOS 12及以下版本,更改背景颜色非常简单:
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
statusBar.backgroundColor = UIColor.black

但对于iOS 13来说,它看起来非常复杂。难道没有类似的方法可以改变它吗?

1
FYI - 那个 iOS 12 的代码从来没有被认为是有效或受支持的,因为它需要挖掘私有 API。所有这样的解决方案,即使它们在某一时刻能够工作,也容易在任何 iOS 更新后失效。 - rmaddy
那么,没有官方的方法可以更改状态栏的背景颜色吗? - jamesxbrown
1个回答

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)
}

这里是参考链接 如何在iOS 7上更改状态栏背景颜色和文本颜色?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接