17
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UINavigationControllerでStatusBar設定を反映する

Posted at

基本的にはViewControllerのprefersStatusBarHiddenがtrueになっていれば良いのだけどUINavigationControllerの場合は上手く行かない場合がある。(恐らく一番上にあるUIViewControllerのステータスバー設定が反映させるために、UINavigationControllerの子ViewControllerは設定が上手く通らないのだと思う)

NavigationController.swift
class NavigationController: UINavigationController {
  private var statusBarStyle = UIStatusBarStyle.Default
  private var statusBarHidden = true
  
  override func viewDidLoad() {
    super.viewDidLoad()
    delegate = self
  }
  
  override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return statusBarStyle
  }
  
  override func prefersStatusBarHidden() -> Bool {
    return statusBarHidden
  }
}

extension NavigationController: UINavigationControllerDelegate {
  func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
    statusBarStyle  = viewController.preferredStatusBarStyle()
    statusBarHidden = viewController.prefersStatusBarHidden()
    setNeedsStatusBarAppearanceUpdate()
  }
  
  func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
    statusBarStyle  = viewController.preferredStatusBarStyle()
    statusBarHidden = viewController.prefersStatusBarHidden()
    setNeedsStatusBarAppearanceUpdate()
  }
}

このようにしてpop/pushの際にそのViewControllerの設定を取得して反映させればおk

17
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?