iOS13 设置状态栏背景颜色
iOS 13 setting status bar background color
我以前有下面的代码,但在升级到 iOS 13 后,我得到以下错误:
UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 94/225, green: 64/255, blue:204/255, alpha: 1.0)
应用在 UIApplication 上调用了 -statusBar 或 -statusBarWindow:必须更改此代码,因为不再有状态栏或状态栏 window。请改用 window 场景中的 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)
}
Kuray 刚刚在这里为我提供了一个解决方案:
https://freakycoder.com/ios-notes-13-how-to-change-status-bar-color-1431c185e845
将以下内容添加到 viewdidload。请前往他的媒介 post 并为他鼓掌!
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
}
代码适用于 Swift 5+ 和 iOS 13+
请参考答案:
向 UIViewController 扩展添加了“statusBarColorChange()”。
func statusBarColorChange() {
if #available(iOS 13.0, *) {
let statusBar = UIView(frame: UIApplication.shared.windows.filter {[=10=].isKeyWindow}.first?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
statusBar.backgroundColor = .appThemeButtonsColor
statusBar.tag = 100
UIApplication.shared.windows.filter {[=10=].isKeyWindow}.first?.addSubview(statusBar)
} else {
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = .appThemeButtonsColor
}
}
希望能有所帮助:)
对于 statusBarFrame 弃用警告,更新了 Daryl Wong 接受的答案:
let window = UIApplication.shared.windows.filter {[=10=].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)
我以前有下面的代码,但在升级到 iOS 13 后,我得到以下错误:
UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 94/225, green: 64/255, blue:204/255, alpha: 1.0)
应用在 UIApplication 上调用了 -statusBar 或 -statusBarWindow:必须更改此代码,因为不再有状态栏或状态栏 window。请改用 window 场景中的 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)
}
Kuray 刚刚在这里为我提供了一个解决方案:
https://freakycoder.com/ios-notes-13-how-to-change-status-bar-color-1431c185e845
将以下内容添加到 viewdidload。请前往他的媒介 post 并为他鼓掌!
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
}
代码适用于 Swift 5+ 和 iOS 13+
请参考答案:
向 UIViewController 扩展添加了“statusBarColorChange()”。
func statusBarColorChange() {
if #available(iOS 13.0, *) {
let statusBar = UIView(frame: UIApplication.shared.windows.filter {[=10=].isKeyWindow}.first?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
statusBar.backgroundColor = .appThemeButtonsColor
statusBar.tag = 100
UIApplication.shared.windows.filter {[=10=].isKeyWindow}.first?.addSubview(statusBar)
} else {
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = .appThemeButtonsColor
}
}
希望能有所帮助:)
对于 statusBarFrame 弃用警告,更新了 Daryl Wong 接受的答案:
let window = UIApplication.shared.windows.filter {[=10=].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)