ios7中UIViewControllerBasedStatusBarAppearance作用详解

2025-05-29 0 94

ios7UIViewControllerBasedStatusBarAppearance详解

在作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:即可,如下代码所示:

?

1

2

3

4
- (BOOL)prefersStatusBarHidden

{

return YES;

}

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 ios7中UIViewControllerBasedStatusBarAppearance作用详解 https://www.kuaiidc.com/91746.html

相关文章

发表评论
暂无评论