標籤:
iOS7中,不僅應用的風格有一定的變化,狀態列變化比較大,我們可以看到UIViewController的狀態列與導覽列基本是一體的。因此UIVIEWCONTROLLER的hide/show狀態的方法也跟其他版本的不一樣了。 在iOS7以前的版本,hide/show是通過以下代碼實現
[cpp] view plain copy
- [[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
- - (UIStatusBarStyle)preferredStatusBarStyle
- {
- return UIStatusBarStyleLightContent;
- //UIStatusBarStyleDefault = 0 黑色文字,淺色背景時使用
- //UIStatusBarStyleLightContent = 1 白色文字,深色背景時使用
- }
-
- - (BOOL)prefersStatusBarHidden
- {
- return NO; //返回NO表示要顯示,返回YES將hiden
- }
上面一個回調方法返回status bar顯示時候的樣式,下面一個回調控制是否顯示status bar.
調用下面的一行代碼將會觸發上面的回調
[cpp] view plain copy
- [self setNeedsStatusBarAppearanceUpdate];
如果想在hiden/show之間有點動畫效果,用下面的代碼即可:
[cpp] view plain copy
- [UIView animateWithDuration:0.5 animations:^{
- [self setNeedsStatusBarAppearanceUpdate];
- }];
或者調用下面的代碼:
[cpp] view plain copy
- [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
在設定好這些,我們還是會發現一些問題,就是狀態列雖然沒有了,但取而代之的是黑色的一片地區,所以我們還需要調整UIViewController的視圖,具體代碼為:
[cpp] view plain copy
- -(void)viewDidLayoutSubviews
- {
- CGRect viewBounds = self.view.bounds;
- CGFloat topBarOffset = 20.0;
- viewBounds.origin.y = -topBarOffset;
- self.view.bounds = viewBounds;
- [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];//for status bar style
- }
iOS之隱藏狀態列