xcode 更改状态栏的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43073623/
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
Changing the Color of the Status Bar
提问by Tyler Rutt
I am trying to change the color of the status bar to like a blue, or some other color.
我正在尝试将状态栏的颜色更改为蓝色或其他颜色。
Is this possible, or does Apple not allow it?
这是可能的,还是苹果不允许?
回答by user3182143
NOTE: This solution fails under iOS 13 and later.
注意:此解决方案在 iOS 13 及更高版本下失败。
First in Plist set View controller-based status bar appearance
to NO
Plist 中的第一个设置View controller-based status bar appearance
为NO
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
statusBar.backgroundColor = UIColor.blue
}
UIApplication.shared.statusBarStyle = .lightContent
return true
}
The output screenshot is below
输出截图如下
回答by byJeevan
No, it's not possible with ready-made public APIs.
不,使用现成的公共 API 是不可能的。
But with the release of iOS 7
, you're allowed to change the appearance of the status bar. Hence I am posting my workaround.
但是随着 的发布iOS 7
,您可以更改状态栏的外观。因此,我发布了我的解决方法。
From an individual view controller by overriding the preferredStatusBarStyle
:
通过覆盖从单个视图控制器preferredStatusBarStyle
:
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
Alternatively, you can set the status bar style by using the UIApplication statusBarStyle
method. To do this, insert a new key named “View controller-based status bar appearance” and set the value to NO.
或者,您可以使用UIApplication statusBarStyle
方法设置状态栏样式。为此,请插入一个名为“基于视图控制器的状态栏外观”的新键,并将值设置为 NO。
By disabling the “View controller-based status bar appearance”, you can set the status bar style by using the following code.
通过禁用“基于视图控制器的状态栏外观”,您可以使用以下代码设置状态栏样式。
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
At the end, change the UINavigationBar
property tint color like below
最后,更改UINavigationBar
属性色调颜色,如下所示
[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];
回答by Krunal
You can set background color for status bar during application launch or during viewDidLoad of your view controller.
您可以在应用程序启动期间或视图控制器的 viewDidLoad 期间设置状态栏的背景颜色。
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
return true
}
}
or
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
}
}
Here is result:
这是结果:
Here is Apple Guidelines/Instructionabout status bar change. Only Dark & light (while & black) are allowed in status bar.
这是有关状态栏更改的Apple 指南/说明。状态栏中只允许深色和浅色(同时和黑色)。
Here is - How to change status bar style:
这是 - 如何更改状态栏样式:
If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance
to NO
in your `.plist' file.
如果你想设置状态栏样式,应用的水平,那么设置UIViewControllerBasedStatusBarAppearance
到NO
你的`的.plist”文件。
if you wan to set status bar style, at view controller level then follow these steps:
如果您想设置状态栏样式,请在视图控制器级别执行以下步骤:
- Set the
UIViewControllerBasedStatusBarAppearance
toYES
in the.plist
file, if you need to set status bar style at UIViewController level only. In the viewDidLoad add function -
setNeedsStatusBarAppearanceUpdate
override preferredStatusBarStyle in your view controller.
- 设置
UIViewControllerBasedStatusBarAppearance
到YES
中.plist
文件,如果需要设置状态栏样式仅UIViewController的水平。 在 viewDidLoad 添加函数 -
setNeedsStatusBarAppearanceUpdate
覆盖视图控制器中的 preferredStatusBarStyle。
-
——
override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Set value of .plist according to status bar style setup level.
根据状态栏样式设置级别设置 .plist 的值。
回答by steveluoxin
Here is my workaround: create a UIView
, add it to your root view of view controller as artificial status bar background
1.Create a UIView
这是我的解决方法:创建一个UIView
,将其添加到视图控制器的根视图中作为人工状态栏背景 1.
创建一个UIView
// status bar's height is 20.0pt
CGRect frame = CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width, 20.0);
UIView *fakeStatusBarBG = [[UIView alloc] initWithFrame:frame];
fakeStatusBarBG.backgroundColor = [UIColor yourColor];
2.Add it to your view controller's root view
2.将其添加到您的视图控制器的根视图
// self is your view controller, make sure fakeStatusBarBG is the top subview in your view hierarchy
[self.view insertSubview:fakeStatusBarBG aboveSubview:yourTopSubview];
There your go.
你去吧。
3.(Additional)Change the content color on status bar, only white or black.
3.(附加)改变状态栏的内容颜色,只有白色或黑色。
- (UIStatusBarStyle)preferredStatusBarStyle
{
if (youWantWhiteColor)
{
return UIStatusBarStyleLightContent;
}
return UIStatusBarStyleDefault;
}
This workaround does not use private API, so you're safe. :-)
此解决方法不使用私有 API,因此您很安全。:-)
回答by Rifat Monzur
I made this extension to change color of status bar
我做了这个扩展来改变状态栏的颜色
public extension UIViewController {
func setStatusBar(color: UIColor) {
let tag = 12321
if let taggedView = self.view.viewWithTag(tag){
taggedView.removeFromSuperview()
}
let overView = UIView()
overView.frame = UIApplication.shared.statusBarFrame
overView.backgroundColor = color
overView.tag = tag
self.view.addSubview(overView)
}
}
Here is usage anywhere in viewcontroller:
这是视图控制器中任何地方的用法:
setStatusBar(color: .red)
setStatusBar(color: .red)