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

查看:255
本文介绍了如何在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 文件中;

  2. 插入

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

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

to -application:didFinishLaunchingWithOptions: of AppDelegate.m。

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 ,在iOS 9中不推荐使用方法 -setStatusBarStyle 。(注意:很久以前,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 中更改了值,就可以调用UIViewController的实例方法 -setNeedsStatusBarAppearanceUpdate -prefersStatusBarHidden

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.

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

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);
}

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

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