一、无导航条的情况:
系统默认状态栏的字体颜色为黑色,即UIStatusBarStyle=UIStatusBarStyleDefault,同时背景颜色和self.view.backgroundColor颜色一致,如下图所示:
![](https://i-blog.csdnimg.cn/blog_migrate/b2c947f21aba047ecd427c6d37987af6.webp?x-image-process=image/format,png)
假如我想让状态栏颜色设置成红色,字体仍为黑色,可以在需要显示的那一页进行如下设置:(最好写在viewWillAppear里面)
//设置状态栏颜色
- (void)setStatusBarBackgroundColor:(UIColor *)color {
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = color;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setStatusBarBackgroundColor:[UIColor redColor]];
self.view.backgroundColor = [UIColor yellowColor];
}
效果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/bc44d9044e166f9cea2891c9ec0f90e7.webp?x-image-process=image/format,png)
假如此时我想让状态栏文字颜色变成白色,可以这样操作:
在上面代码的基础上再添加下面一段代码:
- (UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
效果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/ce216ddd6823935a1fa859f6f8be0cf2.webp?x-image-process=image/format,png)
问题来了,当你在这一页点击按钮进入下一页后,状态栏背景颜色不变,还是红色,而字体颜色却变成黑色了,比较闹心!可以通过下面的方法随心所欲的在任意一页修改状态栏的字体颜色(字体颜色只有白色和黑色)和背景颜色(直接复制到项目中即可
//设置字体颜色
- (UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;//白色
}
//设置状态栏颜色
- (void)setStatusBarBackgroundColor:(UIColor *)color {
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = color;
}
}
//!!!重点在viewWillAppear方法里调用下面两个方法
-(void)viewWillAppear:(BOOL)animated{
[self preferredStatusBarStyle];
[self setStatusBarBackgroundColor:[UIColor redColor]];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
}
下面的效果是第一页要红底白字,第二页要绿底黑字,返回后也是正常显示
![](https://i-blog.csdnimg.cn/blog_migrate/e08f549f0eea4724f074cdc1316b068a.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/53de65c5f70cbbfa42d851042a42307e.webp?x-image-process=image/format,png)
二、有导航条的情况
当我在上面的基础上添加了导航条后,会发现字体颜色由之前的白色变成黑色了,背景颜色倒没有发生变化
![](https://i-blog.csdnimg.cn/blog_migrate/e0f3ab1dec681d0e9039a1737ee48023.webp?x-image-process=image/format,png)
不用担心,可以通过下面的方法完美解决:
//设置状态栏颜色
- (void)setStatusBarBackgroundColor:(UIColor *)color {
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = color;
}
}
-(void)viewWillAppear:(BOOL)animated{
[self setStatusBarBackgroundColor:[UIColor redColor]];
[UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
}
--->!!!同时别忘了在info plist里面将View controller-based status bar appearance设置成NO,(默认是YES)
现在基本的设备都适配ios7以上设备,默认的状态栏字体颜色是黑色
[UIApplicationsharedApplication].statusBarStyle=UIStatusBarStyleDefault;
现在基本的设备都适配ios7以上设备,默认的状态栏字体颜色是黑色
[UIApplicationsharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
![](https://i-blog.csdnimg.cn/blog_migrate/148bd45128ab624350bc6dd9619a9940.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/f701800b214ff12cb708c956a0c7be3c.webp?x-image-process=image/format,png)
Demo下载地址:https://github.com/zhuchenglong/StatusBarDemo
以下是我在实际项目中使用的:
//设置状态栏颜色
- (void)setStatusBarBackgroundColor:(UIColor *)color {
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
NSLog(@"statusBar.backgroundColor--->%@",statusBar.backgroundColor);
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = color;
}
}
- (UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;//白色
}
- (void)viewDidLoad {
[super viewDidLoad];
//Y起点在导航条下面
self.edgesForExtendedLayout = UIRectEdgeNone;
//设置navigationItem返回的文字
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = item;
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//设置导航条透明度
self.navigationController.navigationBar.translucent = NO;//不透明
[[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:1];
//图标颜色为黑色
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
//导航栏背景颜色
[self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
//导航条下面的黑线
self.navigationController.navigationBar.clipsToBounds = NO;
//刷新状态栏背景颜色
// [self setNeedsStatusBarAppearanceUpdate];
//设置状态栏颜色
[self setStatusBarBackgroundColor:[UIColor blackColor]];
}
//一定要在viewWillDisappear里面写,如果写在viewDidDisappear里面会出问题!!!!
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
//为了不影响其他页面在viewDidDisappear做以下设置
self.navigationController.navigationBar.translucent = YES;//透明
[self setStatusBarBackgroundColor:[UIColor clearColor]];
}
效果:
![](https://i-blog.csdnimg.cn/blog_migrate/2d5d08aabc79187c665eed1d6ce0a26b.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/afc27bafabe05ffdcf9db0de282edfb5.webp?x-image-process=image/format,png)
三、UI元素都往上漂移了44pt
iOS 7以后,苹果引入了一个新的属性,叫做[UIViewController setEdgesForExtendedLayout:],它的默认值为UIRectEdgeAll。当你的容器是navigation controller时,默认的布局将从navigation bar的顶部开始。这就是为什么所有的UI元素都往上漂移了44pt。
解决方案一:
edgesForExtendedLayout
默认值不是UIRectEdgeAll
吗,修改为 UIRectEdgeNone
就OK了
注意修改这个属性是要用UIViewController来修改,UINavigationController
,UITabBarController
修改无效。
官方解释:
This property is applied only to view controllers that are embedded in a container such as UINavigationController. The window’s root view controller does not react to this property. The default value of this property is UIRectEdgeAll.
解决方案二:
造成偏移的原因是你的 navigationBar.translucent
默认值为 YES ,此时有半透明效果。如果这个效果是非必须的 改为NO,也可以解决,两者相互独立,互不冲突。
# UIViewController.h
@property(nonatomic,assign) UIRectEdge edgesForExtendedLayout NS_AVAILABLE_IOS(7_0); // Defaults to UIRectEdgeAll
# UINavigationBar.h
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0) UI_APPEARANCE_SELECTOR; // Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent