如何在 iOS 7 上将状态栏的内容颜色设置为白色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18953879/
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 set status bar's content color to white on iOS 7
提问by Kjuly
My App's background colour is black. Cause the whole view is below the status bar on iOS 7, the content on the status bar will hard to be distinguished. So how to change status bar's content colour to white?
我的应用程序的背景颜色是黑色。因为在 iOS 7 上整个视图都在状态栏下方,状态栏上的内容将难以区分。那么如何将状态栏的内容颜色改为白色呢?
I've tried preferredStatusBarStyle
and several other ways, but failed.
我试过preferredStatusBarStyle
和其他几种方法,但都失败了。
回答by Kjuly
- Set "View controller-based status bar appearance” to NOin your info.listfile;
Insert
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
to
-application:didFinishLaunchingWithOptions:
of the AppDelegate.m.
- 在info.list文件中将“基于控制器的状态栏外观”设置为NO;
插入
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
到
-application:didFinishLaunchingWithOptions:
AppDelegate.m。
Note: UIStatusBarStyleDefault
is the default value for the status bar style, it'll show black content instead. Both UIStatusBarStyleBlackTranslucent
& UIStatusBarStyleBlackOpaque
are deprecated in iOS 7.0.
注意:UIStatusBarStyleDefault
是状态栏样式的默认值,它会显示黑色内容。两者UIStatusBarStyleBlackTranslucent
&UIStatusBarStyleBlackOpaque
在 iOS 7.0 中均已弃用。
UPDATE for iOS 9:
iOS 9 更新:
As @ZakariaDarwishmentioned, the method -setStatusBarStyle
is deprecated in iOS 9. (Note: The original question was asked for iOS 7 long time ago, and I don't support it now, the new solution below works for me under iOS 9, hence update here.)
正如@ZakariaDarwish提到的,该方法-setStatusBarStyle
在 iOS 9 中已弃用。(注意:最初的问题是很久以前针对 iOS 7 提出的,我现在不支持它,下面的新解决方案适用于 iOS 9 下的我,因此更新这里。)
So, the only way left (at least for now) is to implement -preferredStatusBarStyle
in your view controller (remember to set "View controller-based status bar appearance" back to YES).
因此,剩下的唯一方法(至少现在)是-preferredStatusBarStyle
在您的视图控制器中实现(记住将“基于视图控制器的状态栏外观”设置回YES)。
You can invoke UIViewController's instance method -setNeedsStatusBarAppearanceUpdate
once value changed in -preferredStatusBarStyle
or -prefersStatusBarHidden
.
-setNeedsStatusBarAppearanceUpdate
一旦值在-preferredStatusBarStyle
或 中更改,您就可以调用 UIViewController 的实例方法-prefersStatusBarHidden
。
There're also two methods -childViewControllerForStatusBarStyle
& -childViewControllerForStatusBarHidden
to return the preferred style from child view controller as you want.
还有两种方法-childViewControllerForStatusBarStyle
&-childViewControllerForStatusBarHidden
可以根据需要从子视图控制器返回首选样式。
e.g., if you used below methods
例如,如果您使用以下方法
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
to switch status bar style before, you can use code sample below
之前切换状态栏样式,您可以使用下面的代码示例
- (void)shouldChangeStatusBarStyleToLightContent:(BOOL)toLightContent
animated:(BOOL)animated
{
_shouldChangeStatusBarStyleToLightContent = toLightContent;
if (animated) {
[UIView animateWithDuration:.3f animations:^{ [self setNeedsStatusBarAppearanceUpdate]; }];
} else {
[self setNeedsStatusBarAppearanceUpdate];
}
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return (_shouldChangeStatusBarStyleToLightContent ? UIStatusBarStyleLightContent : UIStatusBarStyleDefault);
}
for this updated solution now.
现在为这个更新的解决方案。
回答by Satachito
In your *-Info.plist file:
在您的 *-Info.plist 文件中:
- Set 'View controller-based status bar appearance' to NO
- Set 'Status bar style' to UIStatusBarStyleLightContent
- 将“查看基于控制器的状态栏外观”设置为 NO
- 将“状态栏样式”设置为 UIStatusBarStyleLightContent
Alternatively you can specify Status bar style as 'Black Opaque' or 'Black Translucent' in General tab of the Target.(in Xcode 5.0.1) But they are obsoleted values.
或者,您可以在目标的常规选项卡中将状态栏样式指定为“黑色不透明”或“黑色半透明”。(在 Xcode 5.0.1 中)但它们是过时的值。
回答by Denis Kozhukhov
I use this in main controller:
我在主控制器中使用它:
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
回答by Hardik Thakkar
Here a short and simple solution to set status bar color White
这是设置状态栏颜色白色的简短而简单的解决方案
1) First copy this line View controller-based status bar appearance
to in your .plist file and set Boolean NO
;
1)首先将此行复制View controller-based status bar appearance
到您的 .plist 文件中并设置 Boolean NO
;
2) In your AppDelegate.mfile under didFinishLaunchingWithOptionspaste this
2)在你的AppDelegate.m文件中didFinishLaunchingWithOptions粘贴这个
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
OR add in .plist
或添加 .plist
回答by Anthony Marchenko
iOS 9 (deprecated warning workaround)
iOS 9(不推荐使用的警告解决方法)
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
回答by Arash Zeinoddini
#ifdef __IPHONE_7_0
# define STATUS_STYLE UIStatusBarStyleLightContent
#else
# define STATUS_STYLE UIStatusBarStyleBlackTranslucent
#endif
[[UIApplication sharedApplication] setStatusBarStyle:STATUS_STYLE animated:YES];
回答by Rajan Twanabashu
If your application have different status bar's content color for each view controller the preferred method would be implementing
如果您的应用程序对每个视图控制器具有不同的状态栏内容颜色,则首选方法是实现
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
If you need to change the bar's content color globally throughout the application then add following lines of code in your didFinishLaunchingWithOptionsmethod in AppDelegate
如果您需要在整个应用程序中全局更改栏的内容颜色,请在 AppDelegate 的didFinishLaunchingWithOptions方法中添加以下代码行
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarStyle = .lightContent
return true
}
Wait setting the statusBarStyle does nothing if your application is using the default UIViewController-based status bar system. For this
如果您的应用程序使用默认的基于 UIViewController 的状态栏系统,则等待设置 statusBarStyle 没有任何作用。为了这
Set "View controller-based status bar appearance” to NO in your info.list file
在 info.list 文件中将“基于控制器的状态栏外观”设置为 NO
回答by Shawn
Just a note, since this was there. If you are using a UINavigationController, you can throw this into the view controllers viewDidLoad
method:
只是一个说明,因为这是在那里。如果您使用的是 UINavigationController,则可以将其放入视图控制器viewDidLoad
方法中:
self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
回答by smileBot
To do it programmatically in Swift 3 try this anywhere in your view controller.
要在 Swift 3 中以编程方式执行此操作,请在您的视图控制器中的任何位置尝试此操作。
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
I also set the plist key "View controller-based status bar appearance" to YES.
我还将 plist 键“查看基于控制器的状态栏外观”设置为 YES。