第一種方法:
-(void)setRefreshWindow{ CGRect frame = CGRectMake(0.0, 0.0, 320.0, 20.0); statusbarWindow = [[UIWindow alloc] initWithFrame:frame]; [statusbarWindow setBackgroundColor:[UIColor clearColor]]; [statusbarWindow setWindowLevel:UIWindowLevelStatusBar+1.0f]; // 添加自訂子視圖 UIImageView *customView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 0, 120, 18)]; customView.image=[UIImage imageNamed:@"資料重新整理欄.png"]; // UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(100, 0, 100, 20)];// // label.backgroundColor=[UIColor clearColor];// label.text=@"資料正在重新整理";// [customView addSubview:label]; [statusbarWindow addSubview:customView]; [statusbarWindow makeKeyAndVisible];}
第二種方法:
如果需要在狀態列顯示自訂的訊息時,就需要自訂狀態列。
代碼如下:
XYCustomStatusBar.h
03 |
@interface XYCustomStatusBar : UIWindow{ |
05 |
UILabel *_messageLabel; |
08 |
- (void)showStatusMessage:(NSString *)message; |
XYCustomStatusBar.m
01 |
#import "XYCustomStatusBar.h" |
03 |
@implementation XYCustomStatusBar |
07 |
[_messageLabel release], _messageLabel = nil; |
13 |
self.frame = [UIApplication sharedApplication].statusBarFrame; |
14 |
self.backgroundColor = [UIColor blackColor]; |
15 |
self.windowLevel = UIWindowLevelStatusBar + 1.0f; |
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]; |
27 |
- (void)showStatusMessage:(NSString *)message{ |
30 |
_messageLabel.text = @""; |
32 |
CGSize totalSize = self.frame.size; |
33 |
self.frame = (CGRect){ self.frame.origin, 0, totalSize.height }; |
35 |
[UIView animateWithDuration:0.5 animations:^{ |
36 |
self.frame = (CGRect){self.frame.origin, totalSize }; |
37 |
} completion:^(BOOL finished){ |
38 |
_messageLabel.text = message; |
47 |
[UIView animateWithDuration:0.5f animations:^{ |
49 |
} completion:^(BOOL finished){ |
50 |
_messageLabel.text = @""; |
為了讓自訂的狀態列可以讓使用者看到,設定了它的windowlevel,在ios中,windowlevel屬性決定了UIWindow的顯示層次,預設的windowlevel為UIWindowLevelNormal,即0.0 。為了能覆蓋預設的狀態列,將windowlevel設定高點。其他代碼基本上都不解釋什麼,如果要特殊效果,可以自己添加。