码迷,mamicode.com
首页 > 移动开发 > 详细

ios状态栏操作

时间:2015-01-30 16:19:53      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

  1. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; //显示  
  2. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; //隐藏  
让状态栏显示网络等待标志

状态栏是可以通过UIApplication类提供的一些方法来修改的,比如完全去掉状态栏或者修改风格,不过这些改变只是在你的程序内部,当你退出你的程序又会复原。


  1. UIApplication *myApp = [UIapplication sharedApplication];  
复制代码

1.隐藏状态栏


  1. [myApp setStatusBarHidden:YES animated:YES];  
复制代码

记得隐藏状态栏后的你的“桌面”就增加320×20的大小,所以最好是在任何window或者view创建之前隐藏它。

2.状态栏风格

  1. [myApp setStatusBarStyle: UIStatusbarStyleBlackOpaque];  
复制代码
  1. typedef enum {  
  2.         UIStatusBarStyleDefault,  
  3.         UIStatusBarStyleBlackTranslucent,  
  4.         UIStatusBarStyleBlackOpaque  
  5.     } UIStatusBarStyle;  
复制代码
3.状态栏方向
  1. [myApp setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];  
复制代码
  1. typedef enum {  
  2.      UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,  
  3.      //竖屏,垂直向上  
  4.      UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,  
  5.      //竖屏,垂直方向上下颠倒  
  6.      UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,  
  7.      //设备逆时针旋转到横屏模式  
  8.      UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft  
  9.      //设备顺时针旋转到横屏模式  
  10.    } UIInterfaceOrientation;  
复制代码

有时候,需要在状态栏上显示一些自定义信息,比如新浪微博的官方iOS客户端:告知用户信息处于发送队列、发送成功或者发送失败。

技术分享

如上图,通过在状态栏显示自定义信息,可以给用户友好又不影响软件使用的提示。

为此,我们显得定义一个自定义状态栏类,包含一个显示信息的Label:

[cpp]  view plain copy
  1. @interface CustomStatusBar : UIWindow  
  2. {  
  3.     UILabel *_messageLabel;  
  4. }  
  5.   
  6. - (void)showStatusMessage:(NSString *)message;  
  7. - (void)hide;  
  8.   
  9. @end  

接着,设置大小和系统状态栏一致,背景为黑色:
[cpp]  view plain copy
  1. self.frame = [UIApplication sharedApplication].statusBarFrame;  
  2. self.backgroundColor = [UIColor blackColor];  

到这里,为了让自定义的状态栏可以让用户看到,还需要设置它的windowLevel。

在iOS中,windowLevel属性决定了UIWindow的显示层次。默认的windowLevel为UIWindowLevelNormal,即0.0。

系统定义了三个层次如下,具体可参考官方文档

[cpp]  view plain copy
  1. const UIWindowLevel UIWindowLevelNormal;  
  2. const UIWindowLevel UIWindowLevelAlert;  
  3. const UIWindowLevel UIWindowLevelStatusBar;  
  4. typedef CGFloat UIWindowLevel;  

为了能够覆盖系统默认的状态栏,我们把自定义的状态栏的windowLevel调高点:
[cpp]  view plain copy
  1. self.windowLevel = UIWindowLevelStatusBar + 1.0f;  

最后,为显示信息和隐藏添加一点无伤大雅的动画:
[cpp]  view plain copy
  1. - (void)showStatusMessage:(NSString *)message  
  2. {  
  3.     self.hidden = NO;  
  4.     self.alpha = 1.0f;  
  5.     _messageLabel.text = @"";  
  6.       
  7.     CGSize totalSize = self.frame.size;  
  8.     self.frame = (CGRect){ self.frame.origin, 0, totalSize.height };  
  9.       
  10.     [UIView animateWithDuration:0.5f animations:^{  
  11.         self.frame = (CGRect){ self.frame.origin, totalSize };  
  12.     } completion:^(BOOL finished){  
  13.         _messageLabel.text = message;  
  14.     }];  
  15. }  
  16.   
  17. - (void)hide  
  18. {  
  19.     self.alpha = 1.0f;  
  20.       
  21.     [UIView animateWithDuration:0.5f animations:^{  
  22.         self.alpha = 0.0f;  
  23.     } completion:^(BOOL finished){  
  24.         _messageLabel.text = @"";  
  25.         self.hidden = YES;  
  26.     }];;  
  27. }  

ios状态栏操作

标签:

原文地址:http://my.oschina.net/gabriel1215/blog/373528

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!