iOS 状态栏的一些设置
参考:
http://www.cnblogs.com/zhangxueke/p/5881948.html
http://www.2cto.com/kf/201410/342382.html
http://blog.csdn.net/jkwmscq/article/details/47107447
https://my.oschina.net/shede333/blog/304560
http://blog.csdn.net/studying_ios/article/details/51792133
注意:这里讲的是基于iOS7以后的版本
1、关于状态栏的基础知识:
状态栏就是展示“电量”、“网络状态”、“时间”的那一栏。它的高度是20个像素;
状态栏分前后两个部分:
前景部分:显示电池和时间的部分;
背景部分,就是下图中的黄色部分,这里在默认为了透明色,这里我修改为了黄色方便查看。

2、关于前景的设置有两种显示模式
默认的黑色 (UIStatusBarStyleDefault)
白色 (UIStatusBarStyleLightContent)
(1)整个APP的风格是统一的做法
在plist文件中添加 View controller-based status bar appearance,它的默认值是 NO
表示的是由系统管理状态栏,此时在AppDelegate中设置:
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

此时可以将字的颜色变成白色。这里的白色并不会white而是ligh
(2)如果整个App并不希望是统一风格的。可以更加需要修改特定界面的状态栏的前景色
首先:我们需要修改 View controller-based status bar appearance 为 YES 表示的是 讲StatusBar的控制权交给了各自的控制器。
iOS 提供了如下方法:可以在单个界面设置状态条的颜色
-(UIStatusBarStyle)preferredStatusBarStyle;
-(UIViewController *)childViewControllerForStatusBarStyle;
-(void)setNeedsStatusBarAppearanceUpdate
如果你的ViewController都是有Navigation控制的。你确定你的某个界面是需要改变颜色的你可以如下操作:
【题外话:】
//导航条的背景颜色
[self.navigationBar setBarTintColor:[UIColor greenColor]];
//导航条的title字体颜色
[self.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
【正题:】
在UINavigationController中 修改SecondViewController 这个controller的状态栏的颜色会深色的。其他的浅色的。
/这个方法放到Navigation中是可以的,但是在Controller中不被调用/
-(UIStatusBarStyle)preferredStatusBarStyle{
if([self.topViewController isKindOfClass:[SecondViewController class]]){
return UIStatusBarStyleDefault;
}
return UIStatusBarStyleLightContent;
}
这个方法直接放到Controller中不能被调用:
UINavigationController不会将 preferredStatusBarStyle方法调用转给它的子视图,而是由它自己管理状态,而且它也应该那样做.因为UINavigationController 包含了它自己的状态栏
因此就算 UINavigationController中的viewController 实现了 preferredStatusBarStyle方法 也不会调用
如果想把这个方法放到Controller调用到界面去控制状态栏的颜色:
在NavigationController的子类中:
-(UIStatusBarStyle)preferredStatusBarStyle
{
UIViewController * top = self.topViewController;
return [top preferredStatusBarStyle];
}
在Controller的viewDidLoad中调用:
[self setNeedsStatusBarAppearanceUpdate];
注:setNeedsStatusBarAppearanceUpdate 只会调用 navigationController 中的 preferredStatusBarStyle 方法
然后重载:
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
(3)修改状态栏的背景颜色
我使用的方法:
UIView *barView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, self.view.frame.size.width, 20)];
barView.backgroundColor = [UIColor yellowColor];
[self.navigationController.navigationBar addSubview:barView];
因为这个需求并不是很多,状态栏默认的背景颜色是透明的。和设置的导航条的颜色是一样的。
//在info.plist中设置为YES时使用这个隐藏或者显示
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
[self prefersStatusBarHidden];
[self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
}
- (BOOL)prefersStatusBarHidden{
return YES;
}