StatusBar状态栏_01

一、UIViewControllerBasedStatusBarAppearance作用的实际测试

 在作iOS7的适配时,很多文章都会提到UIViewControllerBasedStatusBarAppearance。便一直不是太明白其实际作用。在网上也没有查出明确的说明,苹果的官方文档也是说的不太清楚。在实际测试后发现UIViewControllerBasedStatusBarAppearance的实际作用如下:

        这个属性只影响如何设置status bar上字体的颜色是黑色还是白色,对status bar的背景色无影响。status bar的背景色在iOS7上永远是透明的。

UIViewControllerBasedStatusBarAppearance = NO时:

UIApplication 的setStatusBarStyle方法生效:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

viewController的preferredStatusBarStyle方法无效:
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

UIViewControllerBasedStatusBarAppearance = YES时:

UIApplication 的setStatusBarStyle无效。

viewController的preferredStatusBarStyle方法有效。

 

apple官方说明:

UIViewControllerBasedStatusBarAppearance (Boolean - iOS) specifies whether the status bar appearance is based on the style preferred by the view controller that is currently under the status bar. When this key is not present or its value is set to YES, the view controller determines the status bar style. When the key is set to NO, view controllers (or the app) must each set the status bar style explicitly using the UIApplication object.

google自动翻译的经果:

UIViewControllerBasedStatusBarAppearance(布尔 - IOS)指定状态栏的外观是否是基于视图控制器,是目前在状态栏中的首选风格。当这个键不存在,或者它的值设置为YES时,视图控制器决定了状态栏的风格。当按键被设置为NO,视图控制器(或应用程序)都必须显式使用的UIApplication对象中的每个设置状态栏的风格。

 

二、ios7中让程序使用统一的status bar风格

状态栏 
在iOS7中,状态栏是透明的,就是说,状态栏只有文字没有背景。 
这个改动让我颇为意外,因为一直印象中苹果很care状态栏的,之前也曾听说过有应用因为遮挡了状态栏而被Appstore拒绝。 
而且苹果之前状态栏提供的三种样式都是以深色底白色字呈现,保证了状态栏的内容清晰易读。 

而变透明之后就很容易和后面的内容混淆,虽说一般应用不会把内容和状态栏叠合在一起,但是至少,现在的情况是,默认是会叠合的,开发需要从20px像素以下开始布局页面元素才能避免。 

1、苹果为了让深色浅色背景均能让状态栏内容清晰显示,提供两种状态栏样式: 

需要在Info.plist配置文件中,增加键:UIViewControllerBasedStatusBarAppearance,并设置为NO;

UIStatusBarStyleDefault = 0 黑色文字,浅色背景时使用 
UIStatusBarStyleLightContent = 1 白色文字,深色背景时使用 

而以下两个旧状态栏样式将被废弃: 
UIStatusBarStyleBlackTranslucent = 1 
UIStatusBarStyleLightContent = 2 

2、还有,iOS7中我们通过ViewController重载方法返回枚举值的方法来控制状态栏的隐藏和样式。 
首先,需要在Info.plist配置文件中,增加键:UIViewControllerBasedStatusBarAppearance,并设置为YES; 
然后,在UIViewController子类中实现以下两个方法: 

- (UIStatusBarStyle)preferredStatusBarStyle 

    return UIStatusBarStyleLightContent; 


- (BOOL)prefersStatusBarHidden 

    return NO; 


最后,在需要刷新状态栏样式的时候,调用[self setNeedsStatusBarAppearanceUpdate]方法即可刷新,若果需要以动画形式切换状态栏样式,则用以下方式调用即可: 

[UIView animateWithDuration:0. animations:^{ 
    [self setNeedsStatusBarAppearanceUpdate]; 
}];

 

 

posted on 2016-05-25 21:53  快乐加油站789  阅读(870)  评论(0编辑  收藏  举报

导航