Search code examples
swiftnavbarstatusbar

how to hide status bar and navigation bar when tap device


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.


Solution

  • 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.