2015-11-05 90 views
5

嘗試從模態視圖隱藏狀態欄。隱藏在模態視圖中的狀態欄(在全屏演示文稿中)

已經檢查了幾種方法:

override func prefersStatusBarHidden() -> Bool { 
    return true 
} 

with/without self.setNeedsStatusBarAppearanceUpdate() 

UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: .Fade) 

但在iOS的9

這部作品在全屏演示文稿(模態賽格瑞演示選​​項)貶值,但注意到在整個屏幕這是我的設置。

如果您有任何想法

..

+0

請檢查http://stackoverflow.com/questions/32808593/setstatusbarhidden-withanimation-deprecated-in-ios- 9,我認爲這是你的問題。 – geraldWilliam

+0

嗨,謝謝,但同樣的問題。適用於全屏演示,但不適用於全屏設置。 – raphael

+0

首先進入plist,並檢查基於視圖控制器的狀態欄外觀是否設置爲YES,並設置您嘗試過的prefersStatusBarHidden()。 – Flipper

回答

1

確實FullScreen自動調用狀態欄更新,而不是OverFullScreen

此外,在我來說,我是需要處理堆棧導航控制器,通過ModalViewController爲孩子:

extension UINavigationController { 

    public override func childViewControllerForStatusBarHidden() -> UIViewController? { 
     return self.visibleViewController 
    } 

    public override func childViewControllerForStatusBarStyle() -> UIViewController? { 
     return self.visibleViewController 
    } 
} 

ModalViewController我們手動更新狀態欄,也爲了使它順利我們必須這樣做在viewWillDisappear,但在那一點visibleViewController仍然ModalViewController,沒有什麼可以使用內部布爾statusBarHidden並相應地更新

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 
    self.statusBarHidden = true 
    self.setNeedsStatusBarAppearanceUpdate() 
} 
override func viewWillDisappear(animated: Bool) { 
    super.viewWillDisappear(animated) 
    self.statusBarHidden = false 
    self.setNeedsStatusBarAppearanceUpdate() 
} 
override func prefersStatusBarHidden() -> Bool { 
    return self.statusBarHidden 
} 
30

對於非全屏演示視圖控制器,您需要使用modalPresentationCapturesStatusBarAppearance屬性。

例如

toViewController.modalTransitionStyle = .coverVertical 
toViewController.modalPresentationStyle = .overFullScreen 
toViewController.modalPresentationCapturesStatusBarAppearance = true 

fromViewController.present(toViewController, 
      animated: true, 
      completion: nil) 

對於全屏呈現一個視圖控制器,您需要:

  1. 設置新的VC的modalPresentationStyle
  2. 覆蓋prefersStatusBarHidden在新的VC
  3. 設置你的應用程序的plist UIViewControllerBasedStatusBarAppearance值是

例如

toViewController.modalTransitionStyle = .coverVertical 
toViewController.modalPresentationStyle = .fullScreen 

fromViewController.present(toViewController, 
      animated: true, 
      completion: nil) 

(是的,在iOS的狀態欄設置爲可憐的不錯。這也難怪堆棧溢出,關於這個問題這麼多的問題,這麼多不同的答案。)

+0

'.coverVertical'是否必要? –

0

如果您使用的是故事板和要隱藏/顯示狀態欄,可以查看以前的控制器上使用此方法:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     UIApplication.shared.setStatusBarHidden(false, with: UIStatusBarAnimation.none) 
}