ios开发笔记--状态栏的自定义,隐藏

来源:互联网 发布:手提电脑mac地址怎么查 编辑:程序博客网 时间:2024/04/29 23:15

iOS7 StatusBar 在需要隐藏或改变样式时在UIViewConroller中调用:

[objc] view plain copy
  1. [self setNeedsStatusBarAppearanceUpdate];  

1、隐藏

StatusBar在iOS7中无法使用一下接口隐藏:

[objc] view plain copy
  1. [[UIApplication sharedApplication] setStatusBarHidden:YES];  

若要隐藏需要在UIViewController中实现下列函数:

[objc] view plain copy
  1. - (BOOL)prefersStatusBarHidden  
  2. {  
  3.     return YES;  
  4. }  

2、样式改变

iOS 7中statusbar 有两种样式:白色字体UIStatusBarStyleLightContent和黑色字体UIStatusBarStyleDefault。
如果改变需要在UIViewController中实现:

[objc] view plain copy
  1. - (UIStatusBarStyle)preferredStatusBarStyle  
  2. {  
  3.     return UIStatusBarStyleDefault;  
  4. }  

3、自定义状态栏,方法一

[objc] view plain copy
  1. UIWindow * statusWindow = [[UIWindow alloc] initWithFrame:[UIApplication sharedApplication].statusBarFrame];  
  2.     [statusWindow setWindowLevel:UIWindowLevelStatusBar + 1];  
  3.     [statusWindow setBackgroundColor:[UIColor clearColor]];  
  4.       
  5.     UILabel * statusLabel = [[UILabel alloc] initWithFrame:statusWindow.bounds];  
  6.     statusLabel.text = @"RSSI:";  
  7.     statusLabel.textColor = [UIColor blueColor];  
  8.     statusLabel.textAlignment = NSTextAlignmentCenter;  
  9.     statusLabel.backgroundColor = [UIColor blackColor];  
  10.       
  11.     [statusWindow addSubview:statusLabel];  
  12.       
  13.     [statusWindow makeKeyAndVisible];  

4、自定义状态栏,方法二

如果需要在状态栏显示自定义的消息时,就需要自定义状态栏。

代码如下:

XYCustomStatusBar.h

[objc] view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface XYCustomStatusBar : UIWindow{  
  4.       
  5.     UILabel *_messageLabel;  
  6. }  
  7.   
  8. - (void)showStatusMessage:(NSString *)message;  
  9.   
  10. - (void)hide;  
  11.   
  12. @end  

XYCustomStatusBar.m

[objc] view plain copy
  1. #import "XYCustomStatusBar.h"  
  2.   
  3. @implementation XYCustomStatusBar  
  4.   
  5. - (void)dealloc{  
  6.     [super dealloc];  
  7.     [_messageLabel release], _messageLabel = nil;  
  8. }  
  9.   
  10. - (id)init{  
  11.     self = [super init];  
  12.     if (self) {  
  13.         self.frame = [UIApplication sharedApplication].statusBarFrame;  
  14.         self.backgroundColor = [UIColor blackColor];  
  15.         self.windowLevel = UIWindowLevelStatusBar + 1.0f;  
  16.           
  17.         _messageLabel = [[UILabel alloc] initWithFrame:self.bounds];  
  18.         [_messageLabel setTextColor:[UIColor whiteColor]];  
  19.         [_messageLabel setTextAlignment:NSTextAlignmentRight];  
  20.         [_messageLabel setBackgroundColor:[UIColor clearColor]];  
  21.         [self addSubview:_messageLabel];  
  22.     }  
  23.       
  24.     return self;  
  25. }  
  26.   
  27. - (void)showStatusMessage:(NSString *)message{  
  28.     self.hidden = NO;  
  29.     self.alpha = 1.0f;  
  30.     _messageLabel.text = @"";  
  31.       
  32.     CGSize totalSize = self.frame.size;  
  33.     self.frame = (CGRect){ self.frame.origin0, totalSize.height };  
  34.       
  35.     [UIView animateWithDuration:0.5 animations:^{  
  36.         self.frame = (CGRect){self.frame.origin, totalSize };  
  37.     } completion:^(BOOL finished){  
  38.         _messageLabel.text = message;  
  39.     }];  
  40.       
  41. }  
  42.   
  43.   
  44. - (void)hide{  
  45.     self.alpha = 1.0f;  
  46.       
  47.     [UIView animateWithDuration:0.5f animations:^{  
  48.         self.alpha = 0.0f;  
  49.     } completion:^(BOOL finished){  
  50.         _messageLabel.text = @"";  
  51.         self.hidden = YES;  
  52.     }];  
  53. }  
  54.   
  55. @end  

为了让自定义的状态栏可以让用户看到,设置了它的windowlevel,在ios中,windowlevel属性决定了UIWindow的显示层次,默认的windowlevel为UIWindowLevelNormal,即0.0 。为了能覆盖默认的状态栏,将windowlevel设置高点。其他代码基本上都不解释什么,如果要特殊效果,可以自己添加。
0 0
原创粉丝点击