iOS之隱藏狀態列

來源:互聯網
上載者:User

標籤:

iOS7中,不僅應用的風格有一定的變化,狀態列變化比較大,我們可以看到UIViewController的狀態列與導覽列基本是一體的。因此UIVIEWCONTROLLER的hide/show狀態的方法也跟其他版本的不一樣了。 在iOS7以前的版本,hide/show是通過以下代碼實現

 

[cpp] view plain copy
  1. [[UIApplication sharedApplication] setStatusBarHidden:YES(NO) withAnimation:UIStatusBarAnimationSlide];  


在iOS7中預設情況下,這個方法不成功了。到setStatusBarHidden:withAnimation:聲明的標頭檔去看看,多了一句注釋: // Setting statusBarHidden does nothing if your application is using the default UIViewController-based status bar system. 現在在iOS7中,status bar的外觀預設依賴UIViewController, 也就是說status bar隨UIViewController的不同而不同。在這種預設的方式下,用全域的方法setStatusBarHidden:withAnimation:是行不通的。

google一下發現現在的解決方案有兩種:

如果只是單純的隱藏狀態列,那麼是在預設情況下,只需要重新實現兩個新方法

 

[cpp] view plain copy
  1. - (UIStatusBarStyle)preferredStatusBarStyle  
  2. {  
  3.     return UIStatusBarStyleLightContent;  
  4.     //UIStatusBarStyleDefault = 0 黑色文字,淺色背景時使用  
  5.   //UIStatusBarStyleLightContent = 1 白色文字,深色背景時使用  
  6. }  
  7.   
  8. - (BOOL)prefersStatusBarHidden  
  9. {  
  10.     return NO; //返回NO表示要顯示,返回YES將hiden  
  11. }  



上面一個回調方法返回status bar顯示時候的樣式,下面一個回調控制是否顯示status bar.

調用下面的一行代碼將會觸發上面的回調

 

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


如果想在hiden/show之間有點動畫效果,用下面的代碼即可:

 

[cpp] view plain copy
  1. [UIView animateWithDuration:0.5 animations:^{  
  2.         [self setNeedsStatusBarAppearanceUpdate];  
  3.     }];  

或者調用下面的代碼:

 

[cpp] view plain copy
  1. [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];  

在設定好這些,我們還是會發現一些問題,就是狀態列雖然沒有了,但取而代之的是黑色的一片地區,所以我們還需要調整UIViewController的視圖,具體代碼為:

 

[cpp] view plain copy
    1. -(void)viewDidLayoutSubviews  
    2. {  
    3.     CGRect viewBounds = self.view.bounds;  
    4.     CGFloat topBarOffset = 20.0;  
    5.     viewBounds.origin.y = -topBarOffset;  
    6.     self.view.bounds = viewBounds;  
    7.     [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];//for status bar style  

iOS之隱藏狀態列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.