ios - 在没有导航栏的情况下更改状态栏背景颜色(iOS 13)
问题描述
我正在寻找一种方法来更改状态栏的背景颜色。有类似的问题,但都需要一个导航栏。我找到了这个答案,但这没有任何意义。他使用已弃用的属性。
对于 iOS 12 及更低版本,更改背景颜色非常简单:
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
statusBar.backgroundColor = UIColor.black
但对于 iOS 13,它看起来真的很复杂。没有类似的方法可以改变它吗?
解决方案
您可以使用以下方式更改状态栏颜色。它适用于 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?
推荐阅读
- openvms - OpenVMS - 尝试为无效选择创建操作
- mysql - 有人搜索 NULL 对象时的响应
- php - 如何从视图将参数传递给模型函数?
- powershell - 如何在PowerShell中的多个文件夹中递归更改名称
- python - 在“基线”图上绘制点,每次迭代
- angular - Angular-SpringBoot 下载 excel 文件 HttpErrorResponse
- c++ - 在 C++ 中创建 pacman 碰撞检测的困难
- jquery - 将输入中的数字附加到href
- php - 如何使用 PHP PDO 跨平台进行用户名/密码管理
- google-apps-script - 如果工作表名称已经存在,请在单元格中输入一条消息