27

How to hide status bar and navigation bar when I tap the device like photos in iphone? I had used

UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Slide)

but it's not working.

Edit: I'd like to hide and show status bar and navigation bar, not permanently hide it.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Aditya Dharma
  • 708
  • 2
  • 8
  • 22

3 Answers3

128

With Swift 5 and iOS 12, according to you needs, you may select one of the three following code snippets in order to solve your problem.


#1. Using UINavigationController hidesBarsOnTap property + UIViewController prefersStatusBarHidden and preferredStatusBarUpdateAnimation properties

Since iOS 8, UINavigationController has a hidesBarsOnTap property. hidesBarsOnTap has the following declaration:

var hidesBarsOnTap: Bool { get set }

A Boolean value indicating whether the navigation controller allows hiding of its bars using a tap gesture.

Apple also states about hidesBarsOnTap:

When the value of this property is true, the navigation controller toggles the hiding and showing of its navigation bar and toolbar in response to an otherwise unhandled tap in the content area. The default value of this property is false.

The following code shows how to implement hidesBarsOnTap:

import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationController?.hidesBarsOnTap = true
    }
    
    override var prefersStatusBarHidden: Bool {
        return navigationController?.isNavigationBarHidden == true
    }
    
    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
        return UIStatusBarAnimation.slide
    }
    
}

#2. Using UINavigationController setNavigationBarHidden(_:animated:) method + UIViewController prefersStatusBarHidden and preferredStatusBarUpdateAnimation properties with a UIButton

UINavigationController has a method called setNavigationBarHidden(_:animated:). setNavigationBarHidden(_:animated:) has the following declaration:

func setNavigationBarHidden(_ hidden: Bool, animated: Bool)

Sets whether the navigation bar is hidden.

The following code shows how to toggle your status bar and navigation bar by using setNavigationBarHidden(_:animated:) with a UIButton set in the Storyboard and linked to a @IBAction:

import UIKit

class ViewController: UIViewController {
    
    // Link this @IBAction to a `UIButton`
    @IBAction func toggle(_ sender: UIButton) {
        navigationController?.setNavigationBarHidden(navigationController?.isNavigationBarHidden == false, animated: true)
    }
    
    override var prefersStatusBarHidden: Bool {
        return navigationController?.isNavigationBarHidden == true
    }
    
    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
        return UIStatusBarAnimation.slide
    }
    
}

#3. Using UINavigationController setNavigationBarHidden(_:animated:) method + UIViewController prefersStatusBarHidden and preferredStatusBarUpdateAnimation properties with a UIGestureRecognizer

As an alternative to the previous code, you can use setNavigationBarHidden(_:animated:) with a UIGestureRecognizer instead of a UIButton:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let gesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.toggle))
        view.isUserInteractionEnabled = true
        view.addGestureRecognizer(gesture)
    }

    @objc func toggle() {
        navigationController?.setNavigationBarHidden(navigationController?.isNavigationBarHidden == false, animated: true)
    }

    override var prefersStatusBarHidden: Bool {
        return navigationController?.isNavigationBarHidden == true
    }

    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
        return UIStatusBarAnimation.slide
    }

}

  • Make sure that UIViewControllerBasedStatusBarAppearance is set to true in your project's Info.plist, otherwise the previous sample codes won't work.
  • See this answer for a similar question if you need to target iOS 10.
Community
  • 1
  • 1
Imanou Petit
  • 89,880
  • 29
  • 256
  • 218
  • 2
    Very impressive. I like this a lot. Worked well for me – LondonGuy Mar 11 '15 at 18:44
  • This is a great solution. I am using this for an image that is full screen, however when the navigation bar and toolbar animate in, it changes the position of my image. How do i fix this? Note: My image is inside a scrollview, and my scrollview is the size of the screen without the navigation and toolbar included – rohaldb Jan 19 '16 at 00:02
  • 6
    When using `prefersStatusBarHidden()` with the navigation-bar's visibility, the **navigation-bar** won't animate (slide) when hiding, but still animates the slide, when being shown. Didn't find a way to fix this, yet. – Felix Lieb Jun 21 '16 at 16:54
  • 2
    @rohaldb I have the same issue, did you found a solution? EDIT: solutions is to use `automaticallyAdjustsScrollViewInsets = false` –  Jun 28 '17 at 13:01
  • @FelixLieb Did you found a solution? –  Jun 28 '17 at 13:03
0

On iOS 8 and above you can simply use hidesBarsOnTap:

    navigationController?.hidesBarsOnTap = true
Cooliopas
  • 138
  • 1
  • 5
0

It's appalling that this issue still persists, 6 years later. I've filed a bug as described in this answer. Your best bet bet is to use the old (deprecated in iOS 9) API the OP mentioned in the question, combined with UINavigationController.setNavigationBarHidden(). Or you sacrifice the animation.

Ortwin Gentz
  • 52,648
  • 24
  • 135
  • 213