版本:
OS X 10.10.5
Xcode 6.4(6E35b)
iOS >= 7

一、概述

状态栏(UIStatusBar)指iPhone/iPad/iPod屏幕顶部用于显示网络、时间和电量等的、高度为20点的控件。状态栏的windowLevel为UIWindowLevelStatusBar,而window的windowLevel为UIWindowLevelNormal。所以一般情况下,状态栏位于window之上。

二、UIStatusBar的位置和尺寸

 NSString *statusBarFrame = NSStringFromCGRect([UIApplication sharedApplication].statusBarFrame);
NSLog(@"%@", statusBarFrame);
在iPhone 6竖屏测试输出:
2015-08-04 16:33:47.159 Test[6175:205261] {{0, 0}, {375, 20}}
在iPhone 6横屏测试输出:
2015-08-04 16:33:47.159 Test[6175:205261] {{0, 0}, {667, 20}}
在iPhone 6 Plus竖屏测试输出:
2015-08-04 16:33:47.159 Test[6175:205261] {{0, 0}, {414, 20}}
 
可见其中origin.x和origin.y总是0,size.height总是20,size.width依赖于不同设备及横竖屏。

三、UIStatusBarStyle(字体颜色)和背景颜色

UIStatusBarStyle控制状态栏的字体颜色,在iOS7只支持两种:UIStatusBarStyleDefault、UIStatusBarStyleLightContent。注意,虽然目前表现出来的颜色是黑色或白色,但不是Black或White之类的,苹果留了一手以防以后改变。Default表示深色(Dark),用于亮色(Light)背景;LightContent表示亮色(Light),用于深色(Dark)背景。当然这也不是强制的。
在没有导航栏的情况下,状态栏的背景颜色是透明的,可以在View里添加一个20点高度的子View“伪造”一个背景;在有导航栏的情况下,状态栏的背景颜色和状态栏一样,看起来融为了一体。

四、App启动时状态栏控制

App启动的时候系统加载需要一定的时间,可以给App提供了Launch Image或Launch Screen以增强用户体验。在启动页显示出来的时候App还没有运行,也就谈不上在程序中控制状态栏的字体颜色、显示或隐藏。
默认情况下状态栏是显示出来的,并且Style为UIStatusBarStyleDefault,即黑色。

1、隐藏

可以在Info中将Status bar is initially hidden(UIStatusBarHidden)对应的Value设置为Yes。
也可以在General中将Hide status bar勾选:
实际上,上面两种设置方法最终作用到info.plist文件。可以直接修改该文件,如果不嫌麻烦又不担心出错的话。如果没有使用基于ViewController的状态栏控制,并且App内部又需要将状态栏显示出来,可以在AppDelegate中设置:[[UIApplication sharedApplication] setStatusBarHidden:NO];

2、设置字体颜色为白色

可以在Info中将Status bar style(UIStatusBarStyle)对应的Value设置为UIStatusBarStyeLightContent。
也可以在General中将Status Bar style选择为Light:
同样的,上面两种设置方法最终作用到info.plist文件。如果没有使用基于ViewController的状态栏控制,并且App内部又需要将状态栏颜色改为黑色,可以在AppDelegate中设置:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];

五、App运行时状态栏控制

新建一个Xcode项目,App默认是基于ViewController的状态栏控制,即在ViewController重载prefersStatusBarHidden、preferredStatusBarStyle和preferredStatusBarUpdateAnimation三个方法,及在必要时调用setNeedsStatusBarAppearanceUpdate方法。
如果要使用iOS7之前的通过UIApplication控制状态栏,需在target的info.plist中增加一条View controller-based status bar appearance(UIViewControllerBasedStatusBarAppearance)并设置为NO。
 

1、View controller-based status bar appearance : YES 或 info.plist无此条目

 
UIViewController方法 说明
- (BOOL)prefersStatusBarHidden NS_AVAILABLE_IOS(7_0); // Defaults to NO 询问是否隐藏状态栏。
- (UIStatusBarStyle)preferredStatusBarStyle NS_AVAILABLE_IOS(7_0); // Defaults to UIStatusBarStyleDefault 询问状态栏样式(UIStatusBarStyleDefault/UIStatusBarStyleLightContent)。
// Override to return the type of animation that should be used for status bar changes for this view controller. This currently only affects changes to prefersStatusBarHidden.
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation NS_AVAILABLE_IOS(7_0); // Defaults to UIStatusBarAnimationFade
询问状态栏显示或隐藏动画。
// This should be called whenever the return values for the view controller's status bar attributes have changed. If it is called from within an animation block, the changes will be animated along with the rest of the animation block.
- (void)setNeedsStatusBarAppearanceUpdate NS_AVAILABLE_IOS(7_0);
设置需要更新状态栏。主动调用该方法,将间接调用上述三个方法。如果需要动画生效,需:
    [UIView animateWithDuration:0.4
                     animations:^{
                         [self setNeedsStatusBarAppearanceUpdate];
                     }];
   
 

2、View controller-based status bar appearance : NO

 
UIApplication方法/属性 说明
// Setting statusBarHidden does nothing if your application is using the default UIViewController-based status bar system.
@property(nonatomic,getter=isStatusBarHidden) BOOL statusBarHidden;
- (void)setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation NS_AVAILABLE_IOS(3_2);
设置是否隐藏状态栏。
// Setting statusBarHidden does nothing if your application is using the default UIViewController-based status bar system.
@property(nonatomic) UIStatusBarStyle statusBarStyle; // default is UIStatusBarStyleDefault
- (void)setStatusBarStyle:(UIStatusBarStyle)statusBarStyle animated:(BOOL)animated;
设置状态栏样式(UIStatusBarStyleDefault/UIStatusBarStyleLightContent)。
   
 
如果要在App启动时和运行时全程隐藏状态栏,在View controller-based status bar appearance为NO的情况下,只需简单将Status bar is initially hidden(UIStatusBarHidden)设置为YES。

六、示例代码

可以根据是否是基于ViewController的状态栏控制来决定是否调用UIApplication中控制状态栏的相关方法,也可以直接调用。因为在基于ViewController的状态栏控制时,调用UIApplication中控制状态栏的相关设置方法会被忽略。
@interface ViewController ()

@property (nonatomic) BOOL statusBarIsHidden;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.statusBarIsHidden = NO;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated]; [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault; [self performSelector:@selector(setStatusBarHidden:) withObject:@(YES) afterDelay:.];
[self performSelector:@selector(setStatusBarHidden:) withObject:@(NO) afterDelay:.];
} - (void)setStatusBarHidden:(BOOL)hidden
{
self.statusBarIsHidden = hidden;
[UIView animateWithDuration:0.4
animations:^{
[self setNeedsStatusBarAppearanceUpdate];
}];
[[UIApplication sharedApplication] setStatusBarHidden:hidden withAnimation:UIStatusBarAnimationSlide];
} - (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleDefault;
} - (BOOL)prefersStatusBarHidden
{
return self.statusBarIsHidden;
} - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation
{
return UIStatusBarAnimationSlide;
} @end
七、自定义状态栏
见参考资料:
 
 

[iOS]关于状态栏(UIStatusBar)的若干问题的更多相关文章

  1. ios上 更改 状态栏(UIStatusBar)

    摘要 ios上 更改状态栏(UIStatusBar)的颜色 ios UIStatusBar statusBar 状态栏 更改状态栏颜色 目录[-] IOS上 关于状态栏的相关设置(UIStatusBa ...

  2. ios上 更改 状态栏(UIStatusBar)的颜色,你值得一看、收藏

    IOS上 关于状态栏的相关设置(UIStatusBar) 知识普及 ios上状态栏 就是指的最上面的20像素高的部分 状态栏分前后两部分,要分清这两个概念,后面会用到: 前景部分:就是指的显示电池.时 ...

  3. IOS 状态栏(UIStatusBar)

    ios上状态栏指的屏幕顶端的20像素高的部分 状态栏分前景和背景两部分 前景部分:就是指的显示电池.时间等部分: 背景部分:就是显示白色或者图片的背景部分: 如下图:前景部分为黑色字体,背景部分为白色 ...

  4. 如何实现 iOS 自定义状态栏

    给大家介绍如何实现 iOS 自定义状态栏 Sample Code: 01 UIWindow * statusWindow = [[UIWindow alloc] initWithFrame:[UIAp ...

  5. iOS设置状态栏样式

    iOS设置状态栏样式可以使用两种方式. 方式一: 直接在需要改变默认状态栏样式的控制器中实现一个方法(其他任何事情都不用做): // 返回状态栏的样式 - (UIStatusBarStyle)pref ...

  6. iOS 更改状态栏、导航栏颜色的几种方法

    ios上状态栏 就是指的最上面的20像素高的部分状态栏分前后两部分,要分清这两个概念,后面会用到: 前景部分:就是指的显示电池.时间等部分:背景部分:就是显示黑色或者图片的背景部分: (一)设置sta ...

  7. iOS开发之状态栏UIStatusBar图标操作

    NSArray *subIcons = [[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] val ...

  8. iOS 修改状态栏preferredStatusBarStyle不执行问题

    一.在老版本的iOS中,状态栏永远都是白色风格.而在iOS 7中,我们可以修改每个view controller中状态栏的外观.通过UIStatusBarStyle常量可以指定状态栏的内容是暗色或亮色 ...

  9. ionic ios上状态栏和app重叠解决方案

    干货文章 ·2018-03-22 01:33:01 官方issues: https://github.com/ionic-team/ionic/issues/13294 解决办法: 1.在 confi ...

随机推荐

  1. [读书笔记]C#学习笔记三: C#类型详解..

    前言 这次分享的主要内容有五个, 分别是值类型和引用类型, 装箱与拆箱,常量与变量,运算符重载,static字段和static构造函数. 后期的分享会针对于C#2.0 3.0 4.0 等新特性进行. ...

  2. paip.提升效率--批量变量赋值 “多元”赋值

    paip.提升效率--批量变量赋值 "多元"赋值 ##石麻是批量变量赋值. 为一组变量赋值. 例子 1 <?php $my_array = array("Dog&q ...

  3. Leetcode 345 Reverse Vowels of a String 字符串处理

    题意:倒置字符串中的元音字母. 用两个下标分别指向前后两个相对的元音字母,然后交换. 注意:元音字母是aeiouAEIOU. class Solution { public: bool isVowel ...

  4. iptables防火墙原理详解

    1. netfilter与iptables Netfilter是由Rusty Russell提出的Linux 2.4内核防火墙框架,该框架既简洁又灵活,可实现安全策略应用中的许多功能,如数据包过滤.数 ...

  5. C#与C++之间类型的对应

    Windows Data Type .NET Data Type BOOL, BOOLEAN Boolean or Int32 BSTR String BYTE Byte CHAR Char DOUB ...

  6. Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源 方法

    一.开篇 这里整合分别采用了Hibernate和MyBatis两大持久层框架,Hibernate主要完成增删改功能和一些单一的对象查询功能,MyBatis主要负责查询功能.所以在出来数据库方言的时候基 ...

  7. Java SimpleDateFormat[转]

    [补充] [转] http://stackoverflow.com/questions/2603638/why-cant-this-simpledateformat-parse-this-date-s ...

  8. Why数学图像生成工具

    该软件能够以给定的数学公式及算法生成各种绚烂的数学图像.软件中有两种生成图像的方法: (1)通过一种我自定义的脚本语言生成: 软件中定义一套简单易学的脚本语言,用于描述数学表达式.使用时需要先要将数学 ...

  9. 用Canvas写一个炫酷的时间更新动画玩玩

    正文必须要写点什么...   // '; var WINDOW_WIDTH = 913; var WINDOW_HEIGHT = 400; var RADIUS = 7; //球半径 var NUMB ...

  10. ubuntu下安装多版本Python

    今天一不小心又把ubuntu系统给完坏了,因为我把python3卸载了,然后就...好了,不废话了,接下来就说一下如何在ubuntu下管理python的多个版本.我这里使用的是一个叫pyenv的Pyt ...