一、无导航条的情况:

系统默认状态栏的字体颜色为黑色,即UIStatusBarStyle=UIStatusBarStyleDefault,同时背景颜色和self.view.backgroundColor颜色一致,如下图所示:

14F49066-52A9-4892-AF66-D2F9ED0D9001.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];
}

效果如下:

50B2AB94-49CE-49AB-87A0-E6326C1CE00B.png

假如此时我想让状态栏文字颜色变成白色,可以这样操作:
在上面代码的基础上再添加下面一段代码:

- (UIStatusBarStyle)preferredStatusBarStyle{return UIStatusBarStyleLightContent;
}

效果如下:

ED915548-3B47-45EE-AA27-7D068881A946.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];}

下面的效果是第一页要红底白字,第二页要绿底黑字,返回后也是正常显示

9B2DA0CD-C106-4255-BA02-F34F55D59E99.png

50007497-1481-4F1E-9852-81BFD9CA7B9C.png

二、有导航条的情况

当我在上面的基础上添加了导航条后,会发现字体颜色由之前的白色变成黑色了,背景颜色倒没有发生变化

46CA6B3F-45D6-4628-9D34-0967320616D0.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;
EC111815-4423-479F-BCBD-ADAE52F34E7C.png

54BC597D-F8B3-4EB5-BD0C-394AC0439073.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]];
}

效果:

81331546-9FF9-4314-96D7-29798D9034B0.png

13D7939F-E318-471C-9EBC-E1E92E619685.png

作者:来宝
链接:https://www.jianshu.com/p/5c09c2700038
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

ios 状态栏statusBar的背景颜色相关推荐

  1. ios 适配iPhonex时可以改变状态栏statusBar的背景颜色

    一.无导航条的情况: 系统默认状态栏的字体颜色为黑色,即UIStatusBarStyle=UIStatusBarStyleDefault,同时背景颜色和self.view.backgroundColo ...

  2. iOS 设置导航栏背景颜色

    //导航栏背景色UIColor *navBarColor = [UIColor colorWithRGB_au:0xD42D37];if (@available(iOS 15.0, *)) {UINa ...

  3. android7.0隐藏标题栏,大佬,7.0.22设置后只有标题栏和状态栏能改背景颜色,其它的没变化,要怎么处理一下...

    14:04:45 MDwechat Log: 模块加载中... 14:04:45 MDwechat Log: ---------logStackTraces start---------- 14:04 ...

  4. 安卓刘海屏沉浸式(内容绘制到底部,隐藏底部导航栏,状态栏背景颜色为透明)

    纯java实现(只针对比较新的版本,且没有实现字体为黑色): public class MainActivity extends AppCompatActivity {@Overrideprotect ...

  5. android-设置状态栏与标题栏背景

    通过下面记步,可以设置应用的title与系统的通知状态栏系统的背景颜色: 1. 设置系统Theme <style name="AppTheme.Base" parent=&q ...

  6. iOS开发,更改状态栏(StatusBar)文字颜色为白色

    详细实现步骤 1.如图在Info.plist中进行设置,主要用于处理启动画面中状态栏(StatusBar)文字颜色. 2. 在AppDelegate中的 didFinishLaunchingWithO ...

  7. iOS小技能:设置状态栏背景颜色(图片)

    文章目录 引言 I 状态栏背景颜色的适配方案 1.1 使用新的API [statusBarManager] 1.2 适配特色场景:状态是有透明或者半透明的效果的场景 II 通过安全区域高度判断是否Ip ...

  8. iOS 更改状态栏/导航栏颜色的几种实用方法

    实际开发需求: 这样代码之后 导航栏颜色白色,但是状态栏没有显示  这样写之后,导航栏和状态栏都是白色 一. 状态栏的设置 iOS上状态栏就是指的最上面的20像素高的部分,状态栏分前后两部分. 前景部 ...

  9. iOS 修改状态栏、导航条颜色及文字颜色

    在开发过程中,我们总是遇到这样那样的特殊要求,比如在指定页面展示特殊的样式,完全异于整个app的风格,为此有很多办法解决,今天我来记录其中一种,我们互相学习交流.如有不妥还请指正,如有更好的方案,欢迎 ...

最新文章

  1. opencv 眼睛识别 linux,用opencv测试人脸识别眨眼练习及问题
  2. Apache Software Foundation Distribution Directory
  3. sysbench对数据库的性能测试
  4. Shiro相关文章资料
  5. 如何用JavaScript实现获取验证码的效果
  6. oracle自带split函数_Pandas 基本使用(三) — DataFrame.to_dict() 函数基本使用
  7. 华为BGP动态路由协议理论
  8. 分析java中文乱码的原因
  9. 组合框绑定字符串数组的数据 c# 1614236088
  10. 大屏数据可视化效果如何提升
  11. jquery ui 发布jquery.ui-1.6rc4版本,漂亮多了
  12. 快速排序(C#)实现
  13. https 带端口_基于scapy模块的8种端口扫描
  14. 各种激活函数求导公式
  15. plotyy函数_转载:MATLAB关于plotyy函数的使用三例
  16. 对接丰网查询物流信息
  17. 关于Dev-C++下载及国语转换
  18. MinGW最新版本下载
  19. android友盟自定义事件,友盟统计事件添加
  20. Bluetooth技术学习笔记 ——HFP之Call

热门文章

  1. Photoshop.js对图层的基本操作
  2. error: call to implicitly-deleted copy constructor of 编译报错分析
  3. 房产中介租房平台V4.1.98 【优化】优化房源类型别名显示
  4. java redis之jredis操作
  5. 求1000以内素数平均值
  6. Python数据分析——数据建模、数据分类实现过程、常见分类算法
  7. POJ_2104_Kth_(主席树)
  8. vs2017编译错误:C2001 常量中有换行符 C2146 语法错误: 缺少“)”
  9. 【离散数学】二元关系中的对称与反对称
  10. 如何2小时快速洞察一家公司——企业架构基本功