博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS之StatusBar
阅读量:4088 次
发布时间:2019-05-25

本文共 1815 字,大约阅读时间需要 6 分钟。

 

一. StatusBar的前景颜色设置

StatusBar字体默认为黑色, 要想在工程中启动和视图中的状态栏都是白色,用下面的方法简单配置下就行。

1.设置Status Bar Style为Light. (Status bar style    UIStatusBarStyleLightContent)

 

 

 

2.在plist文件中添加 View controller-based status bar appearance = NO。

 

 

 

二. StatusBar的背景颜色设置

如果是导航视图控制器(UINavigationController):

  UIView *statusBar = [[UIView alloc] initWithFrame:CGRectMake(0, -20, Screen_Width, 20)];

    statusBar.backgroundColor = [UIColor blackColor];
    [self.navigationController.navigationBar addSubview:statusBar];
    
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationController.navigationBar.translucent = NO;
 

如果是视图控制器(UIViewController) : 以下两种方法都可以

(1)

 

UIView *statusBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 20)];

statusBar.backgroundColor = [UIColor blackColor];
[self.view addSubview:statusBar];

 

(2)

  // 设置状态栏颜色
  - (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 setStatusBarBackgroundColor:kMainColor];
  }

 

 

 

 

 

 

// update

iOS9 之前 :

全局设置statusBar :
 step1. info.plist 里面加上View controller-based status bar appearance  BOOL值设为NO
(就是把控制器控制状态栏的权限给禁了,用UIApplication来控制。)

step2. 

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]
[[UIApplication sharedApplication]setStatusBarHidden:YES];

但是这种做法在iOS9不建议使用了,建议我们使用吧那个BOOL值设为YES,然后用控制器的方法来管理状态栏比如:

- (UIStatusBarStyle)preferredStatusBarStyle

{
    return UIStatusBarStyleLightContent;
}

如果iOS9之后, 想方便的全局控制statusBar的字体颜色或隐藏, 可以这样处理:

除了设置View controller-based status bar appearance为 NO外,还需要修改【General】—>【Deployment Info】—>【Status Bar Style】。

 

 

 

 

 

你可能感兴趣的文章
乘法逆元
查看>>
STL源码分析----神奇的 list 的 sort 算法实现
查看>>
Linux中用st_mode判断文件类型
查看>>
Ubuntu修改host遇到unable to resolve host
查看>>
路由选择算法
查看>>
Objective-C 基础入门(一)
查看>>
Objective-C 基础入门(三) 读写文件与回调
查看>>
C++ STL标准库与泛型编程(一)概述
查看>>
C++ STL标准库与泛型编程(四)Deque、Queue、Stack 深度探索
查看>>
C++ STL标准库 算法
查看>>
JVM内存模型_Minor GC笔记
查看>>
SpringCloud学习之PassCloud——(一)PassCloud源代码下载
查看>>
Linux下安装Python环境并部署NLP项目
查看>>
Nginx篇-springCloud配置Gateway+Nginx进行反向代理和负载均衡
查看>>
Nginx篇-Nginx配置动静分离
查看>>
缓存篇-Redis缓存失效以及解决方案
查看>>
缓存篇-使用Redis进行分布式锁应用
查看>>
缓存篇-Redisson的使用
查看>>
phpquery抓取网站内容简单介绍
查看>>
找工作准备的方向(4月22日写的)
查看>>