209
votes

I'd like to remove the status bar at the top of the screen.

This does not work:

func application
(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: NSDictionary?)
-> Bool
{
        application.statusBarHidden = true
        return true
}

I've also tried:

func application
(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: NSDictionary?)
-> Bool
{
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

    var controller = UIViewController()
    application.statusBarHidden = true
    controller.setNeedsStatusBarAppearanceUpdate()

    var view = UIView(frame: CGRectMake(0, 0, 320, 568))
    view.backgroundColor = UIColor.redColor()
    controller.view = view

    var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
    label.center = CGPointMake(160, 284)
    label.textAlignment = NSTextAlignment.Center
    label.text = "Hello World"
    controller.view.addSubview(label)

    self.window!.rootViewController = controller
    self.window!.makeKeyAndVisible()
    return true
}
27
Possible duplicate of How to hide a status bar in iOS?Jake Chasan

27 Answers

464
votes

You really should implement prefersStatusBarHidden on your view controller(s):

Swift 3 and later

override var prefersStatusBarHidden: Bool {
    return true
}
101
votes
  1. Go to Info.plist file
  2. Hover on one of those lines and a (+) and (-) button will show up.
  3. Click the plus button to add new key Type in start with capital V and automatically the first choice will be View controller-based status bar appearance.
  4. Add that as the KEY.
  5. Set the VALUE to "NO"
  6. Go to you AppDelegate.swift
  7. Add the code, inside the method

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool {
        application.statusBarHidden = true
        return true
    }
    

DONE! Run your app and no more status bar!

76
votes

Swift 3

In Info.plist set View controller-based status bar appearance to NO

And call UIApplication.shared.isStatusBarHidden = true

43
votes

If you want to hide and bring back the status bar on button tap, while at the time of presenting and dismissing slide-in menu, popups etc, then you can use this method:-

To hide the status bar:-

UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelStatusBar

To bring back the status bar:-

UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelNormal 
41
votes

if you prefer a visual approach rather than coding it use this method: in your info.plist

enter image description here simply add View controller-based status bar appearance to NO

and Status bar is initially hidden as YES

28
votes

Update for iOS 10 / Swift 3.0

No longer a function, now a property...

override var prefersStatusBarHidden: Bool {
    return true
}
27
votes
 override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true);
    navigationController?.navigationBar.hidden = true // for navigation bar hide
    UIApplication.sharedApplication().statusBarHidden=true; // for status bar hide
}
18
votes

Go to your Info.plist and add two Keys:

Go to your Info.plist and add two Keys:

16
votes

in Swift 3.x:

override func viewWillAppear(_ animated: Bool) {
    UIApplication.shared.isStatusBarHidden = true
}
12
votes

So the issue here actually has nothing to do with Swift but just how status bar appearance is handled as of iOS 7.

By Default, view controllers individually control the appearance of the status bar when they are on the screen. If you want to use this method of controlling the status bar, you can override the following methods for whatever view controllers you'd like to modify the appearance for:

prefersStatusBarHidden, preferredStatusBarStyle, preferredStatusBarAnimation,

In your case, you would just implement prefersStatusBarHidden and return true.

The other way would be to control the status bar appearance at the application level. This seems to be what you're actually trying to do (by setting application.statusBarHidden).

In order to make this work, you need to open up your app's Info.plist file and add the key UIViewControllerBasedStatusBarAppearance, and give it a value of NO.

10
votes

Swift 5+

In my case, I need to update the status bar hidden based on some conditions.

Because of this, I create a base controlller BaseViewController which contains new property hideStatusBar.

Other view controllers are sub-class of this base controller. Finally when I want to update the status bar behavior, I only need to change this hideStatusBar value.

class BaseViewController: UIViewController {

    var hideStatusBar: Bool = false {
        didSet {
            setNeedsStatusBarAppearanceUpdate()
        }
    }

    override var prefersStatusBarHidden: Bool {
           return hideStatusBar
    }
}

How to use

final class ViewController: BaseViewController, UIScrollViewDelegate {
    let scrollView = UIScrollView()

    ...

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        UIView.animate(withDuration: 0.3) {
            if scrollView.contentOffset.y > 100 {
                self.hideStatusBar = true
            } else {
                self.hideStatusBar = false
            }
        }
    }
}

Demo

Here is a demo, I'm using UIView.animate(...) to make the transition smoother.

enter image description here

8
votes

I actually figured this out myself. I'll add my solution as another option.

extension UIViewController {
    func prefersStatusBarHidden() -> Bool {
        return true
    }
}
4
votes

Okay, so this become a problem for me since iOS 9 doesn't support any above the method people have mentioned here such as UIApplication.sharedApplication().statusBarHidden = true or

UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.None)

and

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

works but does not provide programable solution where I can change on a condition. (statusBarHidden = true and statusBarHidden = false as we have done before).

Solution to this madness:

By adding to prefersStatusBarHidden() like below you can programmatically control the hide and show of status bar without adding UIViewControllerBasedStatusBarAppearance setting to your info.plist:

var showStatusBar = true

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

private func showStatusBar(enabled: Bool) {
    showStatusBar = enabled
    prefersStatusBarHidden()
}

then use it like this throughout your code:

//Hide Status Bar
showStatusBar(false)

OR

//Show Status Bar
showStatusBar(true)
4
votes

Just to add, when overriding prefersStatusBarHidden method or variable, the View controller-based status bar appearance in Info.plist must be YES, otherwise the override will have no effect

4
votes

in Swift 4.2 it is a property now.

override var prefersStatusBarHidden: Bool {
    return true
}
3
votes

In my case, I was looking for the status bar to hide/show on demand; instead of just when the view loads or disappears.

swift 3.x

//show status bar initially
var showStatusBar = true

//set the parameters
override var prefersStatusBarHidden: Bool {

    if showStatusBar == true {

        //does not prefer status bar hidden
        print("does not prefer status bar hidden")
        return false

    } else {

        //does prefer status bar hidden
        print("does prefer status bar hidden")
    return true

    }
}

//ex: hide status bar and call parameter function again whenever you want
        showStatusBar = false
        setNeedsStatusBarAppearanceUpdate()
3
votes

Swift 5: In the main view controller, or main navigation controller if you have,

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

    override var prefersStatusBarHidden: Bool {
        return false
    }

And "View controller-based status bar appearance" in plist must be YES, otherwise the above code will not be called.

If you want to hide status bar when launching app, "Status bar is initially hidden" in plist must be YES. This can prevent launch image from being distorted when extra blue bar showing on screen top.

2
votes

A solution that works for me; if you want to hide the status bar on a specific view controller while loading:

import UIKit

class ViewController: UIViewController {

private var hideStatusBar: Bool = false

override var prefersStatusBarHidden: Bool {
    return hideStatusBar
}

override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
    return UIStatusBarAnimation.slide
}

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundcolor = .white
    hideStatusBar = true

    UIView.animate(withDuration: 0.3) {
        self.setNeedsStatusBarAppearanceUpdate()
    }
}

Attention: if you set the key "View controller-based status bar appearance" to "NO" in your info.plist the code above doesn't work. You should set the key to "YES" or remove it from info.plist

2
votes

In your project General->Deployment Info->Status bar style select check mark of Hide status bar Note:- it hides status bar throughout application

2
votes

For Swift 4+ try the following code (tested on Swift 4.0, 4.1 - IOS 10, 11) :

override var prefersStatusBarHidden: Bool { return true }

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    // call this func to force preferredStatusBarStyle to be read again.
    setNeedsStatusBarAppearanceUpdate()
}
2
votes

Updated for iOS 13 and Swift 5

If none of the above answers work for you. Check your plist to see if you have this:

"View controller-based status bar appearance"

If so, be sure to set it to YES!!!!!

Then the following code will work.

override var prefersStatusBarHidden: Bool {
    return true
}
1
votes

I'm using Xcode 8.1 (8B62) with a deployment target set to 10.1 and I haven't had much luck with the override options mentioned above. However checking the "Hide status bar" option in Deployment Info did the trick for me.

Project > General

I hope this helps.

1
votes

If you are presenting the view controller modally, try

viewController.hidesBottomBarWhenPushed = true
viewController.modalPresentationCapturesStatusBarAppearance = true
0
votes
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        application.isStatusBarHidden = true
        return true
    }
0
votes

You can use this code in your ViewController Class scope

open override var prefersStatusBarHidden: Bool { return true }
0
votes

In your Project->General->Deployment info

Statusbar Style:--

just marked Hide status Bar(iOS 10)

0
votes

Swift 4

//MARK:- Show Status Bar
UIApplication.shared.isStatusBarHidden = false

//MARK:- Hide Status Bar
UIApplication.shared.isStatusBarHidden = true