如何在 iOS 7 上更改状态栏背景颜色和文本颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19063365/
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 7?
提问by Rejeesh Rajan
My current application runs on iOS 5 and 6.
我当前的应用程序在 iOS 5 和 6 上运行。
The navigation bar is having an orange color and the status bar is having a black background color with white text color. However, when I run the same application on iOS 7, I observe the status bar looks transparent with the same orange background color as the navigation bar and the status bar text color is black.
导航栏为橙色,状态栏为黑色背景色和白色文本颜色。但是,当我在 iOS 7 上运行相同的应用程序时,我观察到状态栏看起来透明,背景颜色与导航栏相同,并且状态栏文本颜色为黑色。
Due to this I'm not able to differentiate between the status bar and the navigation bar.
因此,我无法区分状态栏和导航栏。
How do I make the status bar to look the same as it was in iOS 5 and 6, that is with black background color and white text color? How can I do this programmatically?
如何使状态栏看起来与 iOS 5 和 6 中的一样,即黑色背景颜色和白色文本颜色?如何以编程方式执行此操作?
回答by Warif Akhand Rishi
Warning: It does not work anymore with iOS 13 and Xcode 11.
警告:它不再适用于 iOS 13 和 Xcode 11。
========================================================================
================================================== ======================
I had to try look for other ways. Which does not involve addSubview
on window. Because I am moving up the window when keyboard is presented.
我不得不尝试寻找其他方法。其中不涉及addSubview
窗口。因为当键盘出现时我正在向上移动窗口。
Objective-C
目标-C
- (void)setStatusBarBackgroundColor:(UIColor *)color {
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = color;
}
}
Swift
迅速
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
return
}
statusBar.backgroundColor = color
}
Swift 3
斯威夫特 3
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }
statusBar.backgroundColor = color
}
Calling this form application:didFinishLaunchingWithOptions
worked for me.
调用此表单application:didFinishLaunchingWithOptions
对我有用。
N.B. We have an app in the app store with this logic. So I guess it is okay with the app store policy.
注意我们在应用商店中有一个具有这种逻辑的应用。所以我想应用商店政策没问题。
Edit:
编辑:
Use at your own risk. Form the commenter @Sebyddd
使用风险自负。形成评论者@Sebyddd
I had one app rejected cause of this, while another was accepted just fine. They do consider it private API usage, so you are subject to luck during the reviewing process :) – Sebyddd
我有一个应用程序被拒绝的原因,而另一个被接受了。他们确实认为这是私有 API 使用,因此您在过程中很幸运:) – Sebyddd
回答by Shahid Iqbal
Goto your app info.plist
1) Set View controller-based status bar appearance
to NO
2) Set Status bar style
to UIStatusBarStyleLightContent
Then Goto your app delegate and paste the following code where you set your Windows's RootViewController.
转到您的应用程序info.plist
1) 设置View controller-based status bar appearance
为NO
2) 设置Status bar style
为UIStatusBarStyleLightContent
然后转到您的应用程序委托并将以下代码粘贴到您设置 Windows 的 RootViewController 的位置。
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
view.backgroundColor=[UIColor blackColor];
[self.window.rootViewController.view addSubview:view];
}
Hope it helps.
希望能帮助到你。
回答by Muruganandham K
1) set the UIViewControllerBasedStatusBarAppearance to YES in the plist
1) 在 plist 中将 UIViewControllerBasedStatusBarAppearance 设置为 YES
2) in viewDidLoad do a [self setNeedsStatusBarAppearanceUpdate];
2)在viewDidLoad中做一个 [self setNeedsStatusBarAppearanceUpdate];
3) add the following method:
3)添加如下方法:
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
UPDATE:
also check developers-guide-to-the-ios-7-status-bar
更新:
还要检查开发人员指南到 ios-7-状态栏
回答by Teja Kumar Bethina
While handling the background color of status bar in iOS 7, there are 2 cases
iOS 7中处理状态栏背景色时,有两种情况
Case 1: View with Navigation Bar
案例 1:使用导航栏查看
In this case use the following code in your viewDidLoad method
在这种情况下,在您的 viewDidLoad 方法中使用以下代码
UIApplication *app = [UIApplication sharedApplication];
CGFloat statusBarHeight = app.statusBarFrame.size.height;
UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
statusBarView.backgroundColor = [UIColor yellowColor];
[self.navigationController.navigationBar addSubview:statusBarView];
Case 2: View without Navigation Bar
案例 2:没有导航栏的视图
In this case use the following code in your viewDidLoad method
在这种情况下,在您的 viewDidLoad 方法中使用以下代码
UIApplication *app = [UIApplication sharedApplication];
CGFloat statusBarHeight = app.statusBarFrame.size.height;
UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
statusBarView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:statusBarView];
Source link http://code-ios.blogspot.in/2014/08/how-to-change-background-color-of.html
源码链接http://code-ios.blogspot.in/2014/08/how-to-change-background-color-of.html
回答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
}
回答by Gabriele Petronella
In iOS 7 the status bar doesn't have a background, therefore if you put a black 20px-high view behind it you will achieve the same result as iOS 6.
在 iOS 7 中状态栏没有背景,因此如果你在它后面放一个 20 像素高的黑色视图,你将获得与 iOS 6 相同的结果。
Also you may want to read the iOS 7 UI Transition Guidefor further information on the subject.
此外,您可能需要阅读iOS 7 UI Transition Guide以获取有关该主题的更多信息。
回答by sanjana
Write this in your ViewDidLoad Method:
在你的 ViewDidLoad 方法中写下这个:
if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
self.edgesForExtendedLayout=UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars=NO;
self.automaticallyAdjustsScrollViewInsets=NO;
}
It fixed status bar color for me and other UI misplacements also to a extent.
它也在一定程度上修复了我的状态栏颜色和其他 UI 错位。
回答by user3107409
for the background you can easily add a view, like in example:
对于背景,您可以轻松添加视图,例如:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 20)];
view.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.1];
[navbar addSubview:view];
where "navbar" is a UINavigationBar.
其中“导航栏”是一个 UINavigationBar。
回答by bendytree
Just to add to Shahid's answer - you can account for orientation changes or different devices using this (iOS7+):
只是为了添加 Shahid 的答案 - 您可以使用此(iOS7+)来考虑方向变化或不同的设备:
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
//Create the background
UIView* statusBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, 20)];
statusBg.backgroundColor = [UIColor colorWithWhite:1 alpha:.7];
//Add the view behind the status bar
[self.window.rootViewController.view addSubview:statusBg];
//set the constraints to auto-resize
statusBg.translatesAutoresizingMaskIntoConstraints = NO;
[statusBg.superview addConstraint:[NSLayoutConstraint constraintWithItem:statusBg attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:statusBg.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
[statusBg.superview addConstraint:[NSLayoutConstraint constraintWithItem:statusBg attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:statusBg.superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
[statusBg.superview addConstraint:[NSLayoutConstraint constraintWithItem:statusBg attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:statusBg.superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
[statusBg.superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[statusBg(==20)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(statusBg)]];
[statusBg.superview setNeedsUpdateConstraints];
...
}
回答by Fattie
Here's a total, copy and paste solution, with an
这是一个完整的复制和粘贴解决方案,带有
absolutely correct explanation
绝对正确的解释
of every issue involved.
涉及的每个问题。
With thanks to Warif Akhand Rishi!
感谢Warif Akhand Rishi!
for the amazing find regarding keyPath statusBarWindow.statusBar
. Good one.
对于关于 keyPath 的惊人发现statusBarWindow.statusBar
。不错的。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// handle the iOS bar!
// >>>>>NOTE<<<<<
// >>>>>NOTE<<<<<
// >>>>>NOTE<<<<<
// "Status Bar Style" refers to the >>>>>color of the TEXT<<<<<< of the Apple status bar,
// it does NOT refer to the background color of the bar. This causes a lot of confusion.
// >>>>>NOTE<<<<<
// >>>>>NOTE<<<<<
// >>>>>NOTE<<<<<
// our app is white, so we want the Apple bar to be white (with, obviously, black writing)
// make the ultimate window of OUR app actually start only BELOW Apple's bar....
// so, in storyboard, never think about the issue. design to the full height in storyboard.
let h = UIApplication.shared.statusBarFrame.size.height
let f = self.window?.frame
self.window?.frame = CGRect(x: 0, y: h, width: f!.size.width, height: f!.size.height - h)
// next, in your plist be sure to have this: you almost always want this anyway:
// <key>UIViewControllerBasedStatusBarAppearance</key>
// <false/>
// next - very simply in the app Target, select "Status Bar Style" to Default.
// Do nothing in the plist regarding "Status Bar Style" - in modern Xcode, setting
// the "Status Bar Style" toggle simply sets the plist for you.
// finally, method A:
// set the bg of the Apple bar to white. Technique courtesy Warif Akhand Rishi.
// note: self.window?.clipsToBounds = true-or-false, makes no difference in method A.
if let sb = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView {
sb.backgroundColor = UIColor.white
// if you prefer a light gray under there...
//sb.backgroundColor = UIColor(hue: 0, saturation: 0, brightness: 0.9, alpha: 1)
}
/*
// if you prefer or if necessary, method B:
// explicitly actually add a background, in our app, to sit behind the apple bar....
self.window?.clipsToBounds = false // MUST be false if you use this approach
let whiteness = UIView()
whiteness.frame = CGRect(x: 0, y: -h, width: f!.size.width, height: h)
whiteness.backgroundColor = UIColor.green
self.window!.addSubview(whiteness)
*/
return true
}