How to Properly Change My Status Bar Style in Swift 2/ iOS 9

How to set Status Bar Style in Swift 3

[UPDATED] For Xcode 10+ & Swift 4.2+

This is the preferred method for iOS 7 and higher

In your application's Info.plist, set View controller-based status bar appearance to YES.

Override preferredStatusBarStyle (Apple docs) in each of your view controllers. For example:

override var preferredStatusBarStyle: UIStatusBarStyle {     
return .lightContent
}

If you have preferredStatusBarStyle returning a different preferred status bar style based on something that changes inside of your view controller (for example, whether the scroll position or whether a displayed image is dark), then you will want to call setNeedsStatusBarAppearanceUpdate() when that state changes.

iOS before version 7, deprecated method

Apple has deprecated this, so it will be removed in the future. Use the above method so that you don't have to rewrite it when the next iOS version is released.

If your application will support In your application's Info.plist, set View controller-based status bar appearance to NO.

In appDelegate.swift, the didFinishLaunchingWithOptions function, add:

UIApplication.shared.statusBarStyle = .lightContent

For Navigation Controller

If you use a navigation controller and you want the preferred status bar style of each view controller to be used and set View controller-based status bar appearance to YES in your application's info.plist

extension UINavigationController {
open override var preferredStatusBarStyle: UIStatusBarStyle {
return topViewController?.preferredStatusBarStyle ?? .default
}
}

Change status bar style with animation

It's now a variable you have to override:

override var preferredStatusBarStyle: UIStatusBarStyle
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation

Depending on when you update the status bar, you might also have to call setNeedsStatusBarAppearanceUpdate()

How to change Status Bar text color in iOS

  1. Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file.

  2. In the viewDidLoad do a [self setNeedsStatusBarAppearanceUpdate];

  3. Add the following method:

    - (UIStatusBarStyle)preferredStatusBarStyle
    {
    return UIStatusBarStyleLightContent;
    }

Note: This does not work for controllers inside UINavigationController, please see Tyson's comment below :)

Swift 3 - This will work controllers inside UINavigationController. Add this code inside your controller.

// Preferred status bar style lightContent to use on dark background.
// Swift 3
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

Swift 5 and SwiftUI

For SwiftUI create a new swift file called HostingController.swift

import Foundation
import UIKit
import SwiftUI

class HostingController: UIHostingController<ContentView> {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}

Then change the following lines of code in the SceneDelegate.swift

window.rootViewController = UIHostingController(rootView: ContentView())

to

window.rootViewController = HostingController(rootView: ContentView())

How to change status bar style - iOS 12

Set View controller-based status bar appearance to NO in the info.plist and override preferredStatusBarStyle in each view controller like so:

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

And call setNeedsStatusBarAppearanceUpdate() in your view controller (in viewDidLoad() for example).

Changing the Status Bar Color for specific ViewControllers using Swift in iOS8

After reading all the suggestions, and trying out a few things, I could get this to work for specific viewcontrollers using the following steps :

First Step:

Open your info.plist and insert a new key named "View controller-based status bar appearance" to NO

Second Step (Just an explanation, no need to implement this):

Normally we put the following code in the application(_:didFinishLaunchingWithOptions:)
method of the AppDelegate,

Swift 2

UIApplication.sharedApplication().statusBarStyle = .LightContent

Swift 3

UIApplication.shared.statusBarStyle = .lightContent

but that affects the statusBarStyle of all the ViewControllers.

So, how to get this working for specific ViewControllers - Final Step:

Open the viewcontroller file where you want to change the statusBarStyle and put the following code in viewWillAppear(),

Swift 2

UIApplication.sharedApplication().statusBarStyle = .LightContent

Swift 3

UIApplication.shared.statusBarStyle = .lightContent

Also, implement the viewWillDisappear() method for that specific viewController and put the following lines of code,

Swift 2

override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default

}

Swift 3

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
}

This step will first change the statusBarStyle for the specific viewcontroller and then change it back to default when the specific viewcontroller disappears. Not implementing the viewWillDisappear() will change the statusBarStyle permanently to the new defined value of UIStatusBarStyle.LightContent

Status Bar Style - Swift 3 - change at any time

Found the answer after quite a lot of digging!

Set (in info.plist):

View controller-based status bar appearance = NO

and remove the (in ViewController.swift):

override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.default
}

...

Now you can use (in ViewController.swift):

UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)

And, to initially set the style for each ViewController, use viewDidAppear:

override func viewDidAppear(_ animated: Bool) {
UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: false)
}

Setting statusbarStyle (deprecated in iOS 9.0)

Set your darkMode variable using the same code you have now, then use it in the computed variable that the system is expecting:

var darkMode = false
override var preferredStatusBarStyle : UIStatusBarStyle {
return darkMode ? .default : .lightContent
}

Depending on the context you may need to force a refresh of the screen for it to take effect. You would do that with the following call:

setNeedsStatusBarAppearanceUpdate()

Set status bar style in Xcode 10

What you have been doing was always wrong; it's just that the expected deprecation has finally come.

Do what the error message says. Implement preferredStatusBarStyle in the top-level view controller (or some view controller that it consults). When the value changes, call setNeedsStatusBarAppearanceUpdate so that preferredStatusBarStyle will be consulted again.



Related Topics



Leave a reply



Submit