iOS之StatusBar

 

一. 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】。

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值