hide status bar swift 4

后端 未结 15 1852
无人及你
无人及你 2021-02-04 01:51

I am trying to hide status bar in one of my UIViewControllers (Swift 4).

  • Firstly, I set View controller-based status bar appearance to

相关标签:
15条回答
  • 2021-02-04 02:36

    Need to write the code in the container view controller if it is a child view controller

         override var prefersStatusBarHidden: Bool {
          return true
         }
    
    0 讨论(0)
  • 2021-02-04 02:38

    Try checking Hide Status Bar under the General section of your project's settings.

    0 讨论(0)
  • 2021-02-04 02:41

    You can hide the status bar in any or all of your view controllers just by adding this code:

    override var prefersStatusBarHidden: Bool {
         return true
       }
    

    Any view controller containing that code will hide the status bar by default.

    If you want to animate the status bar in or out, just call setNeedsStatusBarAppearanceUpdate() on your view controller – that will force prefersStatusBarHidden to be read again, at which point you can return a different value. If you want, your call to setNeedsStatusBarAppearanceUpdate() can actually be inside an animation block, which causes the status bar to hide or show in a smooth way.

    0 讨论(0)
  • 2021-02-04 02:43

    If you are presenting the view controller modally, try

    viewController.modalPresentationCapturesStatusBarAppearance = true
    
    0 讨论(0)
  • 2021-02-04 02:46

    You probably found your own solution to this already, but I got it working this way:

    override func viewWillAppear(_ animated: Bool) {
        // Sets the status bar to hidden when the view has finished appearing
        let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
        statusBar.isHidden = true
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        // Sets the status bar to visible when the view is about to disappear
        let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
        statusBar.isHidden = false
    }
    
    0 讨论(0)
  • 2021-02-04 02:46

    When you try to overload statusbar properties for ViewController which in UINavigationStack - you need make extension below

    extension UINavigationController {
      override open var childForStatusBarStyle: UIViewController? {
        return self.topViewController
      }
    }
    

    then your overloaded properties will became active

    0 讨论(0)
提交回复
热议问题