[ios] How do I hide the status bar in a Swift iOS app?

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
}

This question is related to ios iphone ios7 swift

The answer is


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

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


You can use this code in your ViewController Class scope

open override var prefersStatusBarHidden: Bool { return true }

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

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

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

  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!


Go to your Info.plist and add two Keys:

Go to your Info.plist and add two Keys:


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()
}

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
}

If you are presenting the view controller modally, try

viewController.hidesBottomBarWhenPushed = true
viewController.modalPresentationCapturesStatusBarAppearance = true

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)

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


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


Update for iOS 10 / Swift 3.0

No longer a function, now a property...

override var prefersStatusBarHidden: Bool {
    return true
}

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()

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


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


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.


Swift 4

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

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

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.


in Swift 3.x:

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

Swift 3

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

And call UIApplication.shared.isStatusBarHidden = true


In your Project->General->Deployment info

Statusbar Style:--

just marked Hide status Bar(iOS 10)


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 

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.


in Swift 4.2 it is a property now.

override var prefersStatusBarHidden: Bool {
    return true
}

Examples related to ios

Adding a UISegmentedControl to UITableView Crop image to specified size and picture location Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Autoresize View When SubViews are Added Warp \ bend effect on a UIView? Speech input for visually impaired users without the need to tap the screen make UITableViewCell selectable only while editing Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

Examples related to iphone

Detect if the device is iPhone X Xcode 8 shows error that provisioning profile doesn't include signing certificate Access files in /var/mobile/Containers/Data/Application without jailbreaking iPhone Certificate has either expired or has been revoked Missing Compliance in Status when I add built for internal testing in Test Flight.How to solve? cordova run with ios error .. Error code 65 for command: xcodebuild with args: "Could not find Developer Disk Image" Reason: no suitable image found iPad Multitasking support requires these orientations How to insert new cell into UITableView in Swift

Examples related to ios7

Xcode: Could not locate device support files How to Apply Gradient to background view of iOS Swift App How do I hide the status bar in a Swift iOS app? Using Predicate in Swift How do I programmatically set device orientation in iOS 7? What is the height of Navigation Bar in iOS 7? Warning :-Presenting view controllers on detached view controllers is discouraged Color Tint UIButton Image iOS change navigation bar title font and color How to embed small icon in UILabel

Examples related to swift

Make a VStack fill the width of the screen in SwiftUI Xcode 10.2.1 Command PhaseScriptExecution failed with a nonzero exit code Command CompileSwift failed with a nonzero exit code in Xcode 10 Convert Json string to Json object in Swift 4 iOS Swift - Get the Current Local Time and Date Timestamp Xcode 9 Swift Language Version (SWIFT_VERSION) How do I use Safe Area Layout programmatically? How can I use String substring in Swift 4? 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator Safe Area of Xcode 9 The use of Swift 3 @objc inference in Swift 4 mode is deprecated?