如何在 iOS 13 上更改状态栏背景颜色和文本颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/56651245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the status bar background color and text color on iOS 13?
提问by Hugo Alonso
With the arrival of iOS 13statusBar's view is no longer accessible trough:
随着iOS 13的到来,statusBar 的视图不再通过以下方式访问:
value(forKey: "statusBar") as? UIView
Due to:
由于:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.'
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“应用程序在 UIApplication 上调用了 -statusBar 或 -statusBarWindow:必须更改此代码,因为不再有状态栏或状态栏窗口。改为在窗口场景中使用 statusBarManager 对象。
But it's not clear how it should be used for changing colours as keyWindow?.windowScene?.statusBarManager
does not appear to contain anything related to it.
但是不清楚它应该如何用于更改颜色,因为keyWindow?.windowScene?.statusBarManager
它似乎不包含任何与之相关的内容。
I'm compiling my code with (iOS 10, *) compatibility, so I intend to continue using UIKit.
我正在用 (iOS 10, *) 兼容性编译我的代码,所以我打算继续使用 UIKit。
Any ideas regarding this subject?
关于这个主题的任何想法?
回答by isHidden
You can add some conditions or use first one. Just create some extension for UIApplication.
您可以添加一些条件或使用第一个条件。只需为 UIApplication 创建一些扩展。
extension UIApplication {
var statusBarUIView: UIView? {
if #available(iOS 13.0, *) {
let tag = 38482
let keyWindow = UIApplication.shared.windows.filter {extension UIApplication {
var statusBarUIView: UIView? {
if #available(iOS 13.0, *) {
let tag = 3848245
let keyWindow = UIApplication.shared.connectedScenes
.map({extension UINavigationController {
func setStatusBar(backgroundColor: UIColor) {
let statusBarFrame: CGRect
if #available(iOS 13.0, *) {
statusBarFrame = view.window?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero
} else {
statusBarFrame = UIApplication.shared.statusBarFrame
}
let statusBarView = UIView(frame: statusBarFrame)
statusBarView.backgroundColor = backgroundColor
view.addSubview(statusBarView)
}
}
as? UIWindowScene})
.compactMap({UIApplication.shared.statusBarView?.backgroundColor = UIColor.init(red: 243/250, green: 243/250, blue: 243/250, alpha: 1)
})
.first?.windows.first
if let statusBar = keyWindow?.viewWithTag(tag) {
return statusBar
} else {
let height = keyWindow?.windowScene?.statusBarManager?.statusBarFrame ?? .zero
let statusBarView = UIView(frame: height)
statusBarView.tag = tag
statusBarView.layer.zPosition = 999999
keyWindow?.addSubview(statusBarView)
return statusBarView
}
} else {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
}
return nil
}
}
.isKeyWindow}.first
if let statusBar = keyWindow?.viewWithTag(tag) {
return statusBar
} else {
guard let statusBarFrame = keyWindow?.windowScene?.statusBarManager?.statusBarFrame else { return nil }
let statusBarView = UIView(frame: statusBarFrame)
statusBarView.tag = tag
keyWindow?.addSubview(statusBarView)
return statusBarView
}
} else if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
} else {
return nil
}
}
}
UPDATED: Sorry, I don't have enough time to test it in real projects, but it works in "Hello world" app. You can read more info about keyWindowand statusBarFramein order to make it better.
更新:抱歉,我没有足够的时间在实际项目中测试它,但它可以在“Hello world”应用程序中运行。您可以阅读有关keyWindow和statusBarFrame 的更多信息以使其更好。
if #available(iOS 13.0, *) {
let statusBar = UIView(frame: UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
statusBar.backgroundColor = UIColor.init(red: 243/250, green: 243/250, blue: 243/250, alpha: 1)
UIApplication.shared.keyWindow?.addSubview(statusBar)
} else {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.init(red: 243/250, green: 243/250, blue: 243/250, alpha: 1)
}
回答by Alaa Othman
Unfortunately Apple deprecated some of the mentioned methods of accessing the status bar and editing its attributes. You will have to use the The StatusBarManager
object of the WindowScene
. The following method works for iOS 13 and above:
不幸的是,Apple 弃用了一些提到的访问状态栏和编辑其属性的方法。你将不得不使用的StatusBarManager
对象WindowScene
。以下方法适用于 iOS 13 及更高版本:
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 13, *)
{
let statusBar = UIView(frame: (UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame)!)
statusBar.backgroundColor = #colorLiteral(red: 0.2346, green: 0.3456, blue: 0.5677, alpha: 1)
UIApplication.shared.keyWindow?.addSubview(statusBar)
} else {
// ADD THE STATUS BAR AND SET A CUSTOM COLOR
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
statusBar.backgroundColor = #colorLiteral(red: 0.2346, green: 0.3456, blue: 0.5677, alpha: 1)
}
UIApplication.shared.statusBarStyle = .lightContent
}
}
回答by Ajeet
I have encountered this issue before. My application got crash while I run this code using XCode 11 and Swift 5.0.
我以前遇到过这个问题。当我使用 XCode 11 和 Swift 5.0 运行此代码时,我的应用程序崩溃了。
Previous Code:-
以前的代码:-
if (@available(iOS 13, *)) {
let statusBar1 = UIView()
statusBar1.frame = UIApplication.shared.statusBarFrame
statusBar1.backgroundColor = UIColor.red
UIApplication.shared.keyWindow?.addSubview(statusBar1)
}
Just Changed to:-
刚改成:-
if (@available(iOS 13, *))
{
UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;
statusBar.backgroundColor = [UIColor redColor];
[[UIApplication sharedApplication].keyWindow addSubview:statusBar];
}
Now my problem solved. Happy coding.
现在我的问题解决了。快乐编码。
回答by C13
This worked for me in Swift 5
这在 Swift 5 中对我有用
if #available(iOS 13.0, *) {
let window = UIApplication.shared.windows.filter {if (@available(iOS 13, *))
{
UIView *_localStatusBar = [[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager performSelector:@selector(createLocalStatusBar)];
statusBar = [_localStatusBar performSelector:@selector(statusBar)];
}
else
{
statusBar = [[UIApplication sharedApplication] valueForKey:@"statusBar"];
}
.isKeyWindow}.first
// Reference - https://stackoverflow.com/a/57899013/7316675
let statusBar = UIView(frame: window?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
statusBar.backgroundColor = .white
window?.addSubview(statusBar)
} else {
UIApplication.shared.statusBarView?.backgroundColor = .white
UIApplication.shared.statusBarStyle = .lightContent
}
回答by Tajinder singh
Use the following code:
使用以下代码:
func statusBarColorChange(){
if #available(iOS 13.0, *) {
let statusBar = UIView(frame: UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
statusBar.backgroundColor = AppThemeColor
statusBar.tag = 100
UIApplication.shared.keyWindow?.addSubview(statusBar)
} else {
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = AppThemeColor
}
}
}
to achieve this result:
为了达到这个结果:
回答by BASIL BABY
func removeStatusBar(){
if #available(iOS 13.0, *) {
UIApplication.shared.keyWindow?.viewWithTag(100)?.removeFromSuperview()
}
}
回答by Niki
for swift 5.0 I've done this to change background color,
对于 swift 5.0 我这样做是为了改变背景颜色,
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
removeStatusBar()
statusBarColorChange()
}
override func viewDidLoad() {
super.viewDidLoad()
removeStatusBar()
statusBarColorChange()
}
https://medium.com/@trivediniki94/surprises-after-upgrading-to-xcode-11-ios-13-b52b36e05fa8
https://medium.com/@trivediniki94/surprises-after-upgrading-to-xcode-11-ios-13-b52b36e05fa8
回答by Ewane Shen
you can try this
你可以试试这个
@implementation UIApplication (iOS13PorcoDiDio)
- (UIView*) statusBar
{
if([UIDevice getOSVersion] >= 13)
{
const NSUInteger k_TAG_STATUSBAR = 38482458385;
UIView * vStatusBar = [[UIApplication sharedApplication].keyWindow viewWithTag:k_TAG_STATUSBAR];
if(vStatusBar != nil)
return vStatusBar;
else {
UIView *vStatusBar = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].statusBarFrame];
[vStatusBar setTag:k_TAG_STATUSBAR];
[[UIApplication sharedApplication].keyWindow addSubview:vStatusBar];
return vStatusBar;
}
} else if([UIApplication respondsToSelector:@selector(statusBar)])
return (UIView*)[UIApplication sharedApplication].statusBar;
else
return nil;
}
@end
回答by Milan Aghera
Tested and 100% worked for me
经过测试并 100% 为我工作
##代码##remove status bar from the key window
从键窗口中删除状态栏
##代码##in viewDidLoad
and viewWillAppear
call above function
在viewDidLoad
与viewWillAppear
呼叫上述功能
回答by Fabio Napodano
this is an ObjC version of most voted answer for those like me who are still using it:
对于像我这样仍在使用它的人来说,这是最受欢迎的答案的 ObjC 版本:
create a category of UIApplication and add it to your project:
创建一个 UIApplication 类别并将其添加到您的项目中:
##代码##