ios狀態欄操作

  1. [UIApplication sharedApplication].
  2. networkActivityIndicatorVisible = YES; //顯示  

  3. [UIApplication sharedApplication].
  4. 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:

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

接着,設置大小和系統狀態欄一致,背景爲黑色:

  1. self.frame = [UIApplication sharedApplication].statusBarFrame;  
  2. self.backgroundColor = [UIColor blackColor];  

到這裏,爲了讓自定義的狀態欄可以讓用戶看到,還需要設置它的windowLevel。

在iOS中,windowLevel屬性決定了UIWindow的顯示層次。默認的windowLevel爲UIWindowLevelNormal,即0.0。

系統定義了三個層次如下,具體可參考官方文檔

  1. const UIWindowLevel UIWindowLevelNormal;  
  2. const UIWindowLevel UIWindowLevelAlert;  
  3. const UIWindowLevel UIWindowLevelStatusBar;  
  4. typedef CGFloat UIWindowLevel;  

爲了能夠覆蓋系統默認的狀態欄,我們把自定義的狀態欄的windowLevel調高點:

  1. self.windowLevel = UIWindowLevelStatusBar + 1.0f;  

最後,爲顯示信息和隱藏添加一點無傷大雅的動畫:

  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. }  
發佈了22 篇原創文章 · 獲贊 8 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章