ios 状态栏statusBar的背景颜色
一、无导航条的情况:
系统默认状态栏的字体颜色为黑色,即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];
}
效果如下:
假如此时我想让状态栏文字颜色变成白色,可以这样操作:
在上面代码的基础上再添加下面一段代码:
- (UIStatusBarStyle)preferredStatusBarStyle{return UIStatusBarStyleLightContent;
}
效果如下:
问题来了,当你在这一页点击按钮进入下一页后,状态栏背景颜色不变,还是红色,而字体颜色却变成黑色了,比较闹心!可以通过下面的方法随心所欲的在任意一页修改状态栏的字体颜色(字体颜色只有白色和黑色)和背景颜色(直接复制到项目中即可
//设置字体颜色
- (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];}
下面的效果是第一页要红底白字,第二页要绿底黑字,返回后也是正常显示
二、有导航条的情况
当我在上面的基础上添加了导航条后,会发现字体颜色由之前的白色变成黑色了,背景颜色倒没有发生变化
不用担心,可以通过下面的方法完美解决:
//设置状态栏颜色
- (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;
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://www.jianshu.com/p/5c09c2700038
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
ios 状态栏statusBar的背景颜色相关推荐
- ios 适配iPhonex时可以改变状态栏statusBar的背景颜色
一.无导航条的情况: 系统默认状态栏的字体颜色为黑色,即UIStatusBarStyle=UIStatusBarStyleDefault,同时背景颜色和self.view.backgroundColo ...
- iOS 设置导航栏背景颜色
//导航栏背景色UIColor *navBarColor = [UIColor colorWithRGB_au:0xD42D37];if (@available(iOS 15.0, *)) {UINa ...
- android7.0隐藏标题栏,大佬,7.0.22设置后只有标题栏和状态栏能改背景颜色,其它的没变化,要怎么处理一下...
14:04:45 MDwechat Log: 模块加载中... 14:04:45 MDwechat Log: ---------logStackTraces start---------- 14:04 ...
- 安卓刘海屏沉浸式(内容绘制到底部,隐藏底部导航栏,状态栏背景颜色为透明)
纯java实现(只针对比较新的版本,且没有实现字体为黑色): public class MainActivity extends AppCompatActivity {@Overrideprotect ...
- android-设置状态栏与标题栏背景
通过下面记步,可以设置应用的title与系统的通知状态栏系统的背景颜色: 1. 设置系统Theme <style name="AppTheme.Base" parent=&q ...
- iOS开发,更改状态栏(StatusBar)文字颜色为白色
详细实现步骤 1.如图在Info.plist中进行设置,主要用于处理启动画面中状态栏(StatusBar)文字颜色. 2. 在AppDelegate中的 didFinishLaunchingWithO ...
- iOS小技能:设置状态栏背景颜色(图片)
文章目录 引言 I 状态栏背景颜色的适配方案 1.1 使用新的API [statusBarManager] 1.2 适配特色场景:状态是有透明或者半透明的效果的场景 II 通过安全区域高度判断是否Ip ...
- iOS 更改状态栏/导航栏颜色的几种实用方法
实际开发需求: 这样代码之后 导航栏颜色白色,但是状态栏没有显示 这样写之后,导航栏和状态栏都是白色 一. 状态栏的设置 iOS上状态栏就是指的最上面的20像素高的部分,状态栏分前后两部分. 前景部 ...
- iOS 修改状态栏、导航条颜色及文字颜色
在开发过程中,我们总是遇到这样那样的特殊要求,比如在指定页面展示特殊的样式,完全异于整个app的风格,为此有很多办法解决,今天我来记录其中一种,我们互相学习交流.如有不妥还请指正,如有更好的方案,欢迎 ...
最新文章
- opencv 眼睛识别 linux,用opencv测试人脸识别眨眼练习及问题
- Apache Software Foundation Distribution Directory
- sysbench对数据库的性能测试
- Shiro相关文章资料
- 如何用JavaScript实现获取验证码的效果
- oracle自带split函数_Pandas 基本使用(三) — DataFrame.to_dict() 函数基本使用
- 华为BGP动态路由协议理论
- 分析java中文乱码的原因
- 组合框绑定字符串数组的数据 c# 1614236088
- 大屏数据可视化效果如何提升
- jquery ui 发布jquery.ui-1.6rc4版本,漂亮多了
- 快速排序(C#)实现
- https 带端口_基于scapy模块的8种端口扫描
- 各种激活函数求导公式
- plotyy函数_转载:MATLAB关于plotyy函数的使用三例
- 对接丰网查询物流信息
- 关于Dev-C++下载及国语转换
- MinGW最新版本下载
- android友盟自定义事件,友盟统计事件添加
- Bluetooth技术学习笔记 ——HFP之Call
热门文章
- Photoshop.js对图层的基本操作
- error: call to implicitly-deleted copy constructor of 编译报错分析
- 房产中介租房平台V4.1.98 【优化】优化房源类型别名显示
- java redis之jredis操作
- 求1000以内素数平均值
- Python数据分析——数据建模、数据分类实现过程、常见分类算法
- POJ_2104_Kth_(主席树)
- vs2017编译错误:C2001 常量中有换行符 C2146 语法错误: 缺少“)”
- 【离散数学】二元关系中的对称与反对称
- 如何2小时快速洞察一家公司——企业架构基本功