ios7状态栏

现在在iOS7中 status bar 依赖于UIViewController,也就是说状态栏随UIViewController 的变化而变化。

在iOS6中若想隐藏 ststus bar 可直接使用

- (void)setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation

方法。

如果只是单纯的隐藏状态栏,那么是在默认情况下,只需要重新实现两个新方法

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (UIStatusBarStyle)preferredStatusBarStyle  
  2. {  
  3.     return UIStatusBarStyleLightContent;  
  4.     //UIStatusBarStyleDefault = 0 黑色文字,浅色背景时使用  
  5.   //UIStatusBarStyleLightContent = 1 白色文字,深色背景时使用  
  6. }  
  7.   
  8. - (BOOL)prefersStatusBarHidden  
  9. {  
  10.     return NO; //返回NO表示要显示,返回YES将hiden  
  11. }  


第一个回调方法返回status bar显示时候的样式,第二个回调方法控制是否显示status bar.

调用下面的一行代码将会触发上面的回调

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [self setNeedsStatusBarAppearanceUpdate];  

如果想在hiden/show之间有点动画效果,用下面的代码即可:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [UIView animateWithDuration:0.5 animations:^{  
  2.         [self setNeedsStatusBarAppearanceUpdate];  
  3.     }];  

或者调用下面的代码:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];  

还有一种方法是在infor.plist中加入key:UIViewControllerBasedStatusBarAppearance 并设置其值为NO,这样就告诉系统,status bar不依赖于UIViewController. 这样就可以通过上面的方法来hiden status bar.

在设置好这些,我们还是会发现一些问题,就是状态栏虽然没有了,但取而代之的是黑色的一片区域,所以我们还需要调整UIViewController的视图,具体代码为:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. -(void)viewDidLayoutSubviews  
  2. {  
  3.     CGRect viewBounds = self.view.bounds;  
  4.     CGFloat topBarOffset = 20.0;  
  5.     viewBounds.origin.y = -topBarOffset;  
  6.     self.view.bounds = viewBounds;  
  7.     [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];//for status bar style  
  8. }  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值