如何在 iOS 7 上将状态栏的内容颜色设置为白色 [英] How to set status bar's content color to white on iOS 7

查看:24
本文介绍了如何在 iOS 7 上将状态栏的内容颜色设置为白色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的背景颜色是黑色.由于 iOS 7 上整个视图都在状态栏下方,因此很难区分状态栏上的内容.那么如何将状态栏的内容颜色改为白色呢?

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?

我尝试了 preferredStatusBarStyle 和其他几种方法,但都失败了.

I've tried preferredStatusBarStyle and several other ways, but failed.

推荐答案

  1. 在您的 info.list 文件中将查看基于控制器的状态栏外观"设置为 NO
  2. 插入

  1. Set "View controller-based status bar appearance" to NO in your info.list file;
  2. Insert

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

到 AppDelegate.m 的 -application:didFinishLaunchingWithOptions:.

to -application:didFinishLaunchingWithOptions: of the AppDelegate.m.

<小时>

注意:UIStatusBarStyleDefault 是状态栏样式的默认值,它会显示黑色内容.UIStatusBarStyleBlackTranslucent &UIStatusBarStyleBlackOpaque 在 iOS 7.0 中已弃用.


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.

作为 @ZakariaDarwish 提到,-setStatusBarStyle 方法在 iOS 9 中已被弃用.(注:原问题是很久以前针对 iOS 7 提出的,我不支持现在,下面的新解决方案适用于我在 iOS 9 下,因此在此处更新.)

As @ZakariaDarwish mentioned, 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.)

所以,剩下的唯一方法(至少现在)是在您的视图控制器中实现 -preferredStatusBarStyle(记得设置基于视图控制器的状态栏外观" 返回).

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-prefersStatusBarHidden 中的值发生更改,您可以调用 UIViewController 的实例方法 -setNeedsStatusBarAppearanceUpdate.

You can invoke UIViewController's instance method -setNeedsStatusBarAppearanceUpdate once value changed in -preferredStatusBarStyle or -prefersStatusBarHidden.

还有两种方法-childViewControllerForStatusBarStyle &-childViewControllerForStatusBarHidden 根据需要从子视图控制器返回首选样式.

There're also two methods -childViewControllerForStatusBarStyle & -childViewControllerForStatusBarHidden to return the preferred style from child view controller as you want.

例如,如果您使用以下方法

[[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.

这篇关于如何在 iOS 7 上将状态栏的内容颜色设置为白色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆