216

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
}
Michael
  • 32,527
  • 49
  • 210
  • 370
Jay
  • 9,314
  • 7
  • 33
  • 40
  • Possible duplicate of [How to hide a status bar in iOS?](https://stackoverflow.com/questions/12661031/how-to-hide-a-status-bar-in-ios) – Jake Chasan Jul 11 '19 at 15:14

27 Answers27

475

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

Swift 3 and later

override var prefersStatusBarHidden: Bool {
    return true
}
Jake Chasan
  • 6,290
  • 9
  • 44
  • 90
drewag
  • 93,393
  • 28
  • 139
  • 128
  • 5
    I think Jay's intention is to hide the status bar for complete app. That's why he would have written hide functionality in application's didFinishLaunchingWithOptions. How to hide status bar for complete app? – Satyam Dec 13 '14 at 06:34
  • @Satyam has good point, it would be nice to remove this throughout the entire application. Is there an approach to implement this through inheritance? Or via protocol extension? – Dan Beaulieu Feb 13 '16 at 18:07
  • 3
    @DanBeaulieu I think through inheritance would be a great solution. Create a UIViewController subclass where bar hidden is set to true and then make all your subclases inherit from that one. Another approach could be using [Swizzling](http://nshipster.com/swift-objc-runtime/) – crisisGriega Apr 21 '16 at 13:57
  • 1
    Swift 3 code did not work, see: http://stackoverflow.com/a/38902285/129202 – Jonny Feb 22 '17 at 08:55
  • 1
    In this method there is a fault: when you want to perform a segue, your parent view of current viewcontroller fall down about 20 px – iman kazemayni Nov 25 '18 at 13:20
103
  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!

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
nycdanie
  • 2,921
  • 3
  • 18
  • 21
  • 1
    At first I thought this solution worked fine, but then I noticed that it causes an error that I needed to debug with CG_CONTEXT_SHOW_BACKTRACE. Backtraced it to the adding of "View controller-based status bar appearance" – Sean Oct 20 '15 at 20:01
  • Worked fine for me in IOS 9 2.2 – uplearned.com Jul 03 '16 at 05:27
  • 1
    Worked for iOS 10.1 simulator. Thanks, @nycdanie. – Jerome Jan 05 '17 at 08:31
  • 8
    In addition to setting "View controller-based status bar appearance" to NO, also add "Status bar is initially hidden" set to "YES". Then you don't need to add code in the view controller and the status bar will be hidden in the entire application. Xcode 8.1, Swift 3.0.1, iOS 10 – tylerSF Jan 10 '17 at 04:47
  • 1
    Use UIApplication.shared.isStatusBarHidden = true for swift 3 Xcode 8 – oOEric Aug 11 '17 at 03:09
  • 1
    @tylerSF Works great! You should add this as an answer :) – Pétur Ingi Egilsson Feb 05 '18 at 11:27
77

Swift 3

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

And call UIApplication.shared.isStatusBarHidden = true

Codetard
  • 2,441
  • 28
  • 34
Joseph Mark
  • 9,298
  • 4
  • 29
  • 31
44

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 
Vincent Joy
  • 2,598
  • 15
  • 25
  • This is more of a hack. I wouldn't want to meddle with the window like this... especially if a solution already exists. I would encourage developers to override the `prefersStatusBarHidden` property like what has already been mentioned. – Stephen Paul Jun 16 '17 at 21:58
  • 2
    this can be used if we want to hide and bring back the status bar momentarily.. in my app, when the slider menu comes from the left side, i've to hide the status bar. and when the menu disappears, we have to bring back status bar, like in gmail's iOS app.. so in those kind of scenarios, we can use this. – Vincent Joy Jun 17 '17 at 07:10
  • 3
    It IS a hack, and I wouldn't meddle with it, but it _does_ work for the moment. Kind of like you all say. The problem with `prefersStatusBarHidden` is that views tied to the status bar using constraints, and also navigation bars, will move around in a bad fashion if you toggle status bar on/off using `prefersStatusBarHidden `. For the moment only this answer seems to work around that. – Jonny Apr 19 '18 at 06:12
  • Totally agree with @Jonny, I dont like this solution either but like he said, overriding `prefersStatusBarHidden` will mess your constraint. So far this does the work. However I am using a small wrapper to avoid using singletons, you can find it [here](https://gist.github.com/rgkobashi/298a6016662c64a07a12f78a5b6e73ea) – rgkobashi Sep 30 '18 at 07:56
43

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

MiladiuM
  • 1,449
  • 16
  • 17
28
 override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true);
    navigationController?.navigationBar.hidden = true // for navigation bar hide
    UIApplication.sharedApplication().statusBarHidden=true; // for status bar hide
}
Mohit Tomar
  • 5,173
  • 2
  • 33
  • 40
28

Update for iOS 10 / Swift 3.0

No longer a function, now a property...

override var prefersStatusBarHidden: Bool {
    return true
}
Ben
  • 3,832
  • 1
  • 29
  • 30
atlwx
  • 1,334
  • 1
  • 14
  • 9
19

Go to your Info.plist and add two Keys:

Go to your Info.plist and add two Keys:

janaz
  • 673
  • 5
  • 12
16

in Swift 3.x:

override func viewWillAppear(_ animated: Bool) {
    UIApplication.shared.isStatusBarHidden = true
}
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
Samira
  • 844
  • 13
  • 17
13

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

nahung89
  • 7,745
  • 3
  • 38
  • 40
12

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.

Jsdodgers
  • 5,253
  • 2
  • 20
  • 36
Dima
  • 23,484
  • 6
  • 56
  • 83
8

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

extension UIViewController {
    func prefersStatusBarHidden() -> Bool {
        return true
    }
}
Jay
  • 9,314
  • 7
  • 33
  • 40
  • Nice approach to keep things clean and modular – Roger Fernandez Guri Sep 01 '14 at 15:49
  • 2
    I can't implement this. Maybe it's because now I use Swift 1.2. I'm getting the error: "Method 'prefersStatusBarHidden()' with Objective-C selector 'prefersStatusBarHidden' conflicts with previous declaration with the same Objective-C selector". I've also added the override keyword at beginning, but still get the same error. – Andrej Aug 25 '15 at 17:57
  • Do you need to add this to every view? – Sean Oct 20 '15 at 20:03
  • Doesn't work in Swift 2, shows error as explained by @Andrej above. – Nagendra Rao Jan 29 '16 at 18:09
5

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
}
Legolas Wang
  • 1,951
  • 1
  • 13
  • 26
  • 1
    Totally right. Other answers work with "NO" only because the yalso set the "start hidden" value to YES I guess. But we really do want to say that the status bar appearance IS controller-based, NOT system based. This way, the controller can specify prefersStatusBarHidden()... – Benjamin Piette Jan 26 '21 at 02:49
4

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)
CodeOverRide
  • 4,431
  • 43
  • 36
  • 1
    Does `prefersStatusBarHidden` calling make any sense? I guess you mean `self.setNeedsStatusBarAppearanceUpdate()` after `showStatusBar` assign – Leo Apr 09 '16 at 11:44
  • It really is madness, isn't it? What a pitiful API this is, and has been for so long. This kind of thing make iOS development incredibly frustrating at times. – Womble Sep 13 '16 at 01:23
  • @Womble, yes and it can get pretty complicated as well. Hopefully Swift 3.0 has a better library and supports as by first look of it, it's going to change whole lot from swift 2.3...breaking stuff. – CodeOverRide Sep 19 '16 at 23:16
  • Instead of calling prefersStatusBarHidden from your method, you could call setNeedsStatusBarAppearanceUpdate – Oscar Oct 19 '16 at 08:38
4

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

Huanyan
  • 85
  • 1
  • 7
4

in Swift 4.2 it is a property now.

override var prefersStatusBarHidden: Bool {
    return true
}
Rawand Saeed
  • 795
  • 10
  • 13
3

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()
Felecia Genet
  • 367
  • 4
  • 3
3

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()
}
Coder ACJHP
  • 1,940
  • 1
  • 20
  • 34
3

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.

Sam Xu
  • 252
  • 3
  • 5
2

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

andre_hold
  • 562
  • 8
  • 20
  • You cannot override the hideStatusBar property since it's a stored property ! you can however just choose another name and your animation will work. – XcodeNOOB Jul 29 '18 at 12:14
2

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

Sweta Vani
  • 61
  • 9
2

If you are presenting the view controller modally, try

viewController.hidesBottomBarWhenPushed = true
viewController.modalPresentationCapturesStatusBarAppearance = true
YanSte
  • 10,661
  • 3
  • 57
  • 53
1

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.

danmerfeld
  • 11
  • 1
0
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        application.isStatusBarHidden = true
        return true
    }
0

You can use this code in your ViewController Class scope

open override var prefersStatusBarHidden: Bool { return true }
Sajjad
  • 51
  • 7
  • Thank You for the answer, do you care to elaborate a bit more. Where exactly he must add the line of code and why this would work? See the [How do I write a good answer](https://stackoverflow.com/help/how-to-answer) section. – Roan Aug 30 '17 at 05:19
0

In your Project->General->Deployment info

Statusbar Style:--

just marked Hide status Bar(iOS 10)

V D Purohit
  • 1,179
  • 1
  • 10
  • 23
0

Swift 4

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

//MARK:- Hide Status Bar
UIApplication.shared.isStatusBarHidden = true
Shakeel Ahmed
  • 5,361
  • 1
  • 43
  • 34