当前位置: 移动技术网 > 移动技术>移动开发>IOS > ios7中UIViewControllerBasedStatusBarAppearance作用详解

ios7中UIViewControllerBasedStatusBarAppearance作用详解

2019年07月24日  | 移动技术网移动技术  | 我要评论

  ios7中uiviewcontrollerbasedstatusbarappearance详解

在作ios7的适配时,很多文章都会提到uiviewcontrollerbasedstatusbarappearance。便一直不是太明白其实际作用,使用时发现uiviewcontrollerbasedstatusbarappearance的实际作用如下:      

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

apple官方对uiviewcontrollerbasedstatusbarappearance得说明:

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.

即:uiviewcontrollerbasedstatusbarappearance(一个boolean值)指定状态栏的外观是否是基于当前视图控制器给状态栏指定的首选风格。当这个键不存在,或者它的值设置为yes时(也就是说这个key的默认value为yes),视图控制器决定了状态栏的风格。当按键被设置为no,视图控制器(或应用程序)都必须通过uiapplication对象(即uiapplication的setstatusbarstyle方法)显示的设置状态栏风格。

通过apple的官方文档不难看出,在ios7上,设置状态栏风格(暗色或者亮色)的方法无非就两种,第一种是在controller中通过回调方法preferredstatusbarstyle返回状态栏的风格,第二种是通过uiapplication对象的setstatusbarstyle设置, uiviewcontrollerbasedstatusbarappearance实际上是指定了是否优先使用第一种方法(这也就不难理解为什么这个key值叫做uiviewcontrolle “based”)。

所以当plist中没有uiviewcontrollerbasedstatusbarappearance这个key,或者存在这个key,并且value为yes时,
viewcontroller的preferredstatusbarstyle方法对状态栏的设置生效;

当uiviewcontrollerbasedstatusbarappearance对应的value为no时,
[uiapplication sharedapplication] 通过方法setstatusbarstyle对状态栏的设置生效。

隐藏状态栏

有时候我们需要隐藏状态栏,那么此时我们在view controller中override方法prefersstatusbarhidden:即可,如下代码所示:

- (bool)prefersstatusbarhidden 
{ 
  return yes; 
} 

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网