IOS開發之自訂狀態條

來源:互聯網
上載者:User

第一種方法:

-(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

01 #import
02
03 @interface XYCustomStatusBar : UIWindow{
04
05 UILabel *_messageLabel;
06 }
07
08 - (void)showStatusMessage:(NSString *)message;
09
10 - (void)hide;
11
12 @end

XYCustomStatusBar.m

01 #import "XYCustomStatusBar.h"
02
03 @implementation XYCustomStatusBar
04
05 - (void)dealloc{
06 [super dealloc];
07 [_messageLabel release], _messageLabel = nil;
08 }
09
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.origin, 0, 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設定高點。其他代碼基本上都不解釋什麼,如果要特殊效果,可以自己添加。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.