12

I used to use setStatusBarStyle in my project and it works fine, but it is deprecated so I use preferredStatusBarStyle, that didn't work. knowing that I've:

  1. Call the method setNeedsStatusBarAppearanceUpdate.
  2. Set "View controller-based status bar appearance" to NO in info.plist
  3. Override the function

    • (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; }

    this function is not called

Note: I'm using navigation controller.

Krunal
  • 77,632
  • 48
  • 245
  • 261
Sarah
  • 177
  • 1
  • 1
  • 9
  • https://stackoverflow.com/questions/38740648/how-to-set-status-bar-style-in-swift-3 – Michael Ramos Jul 28 '17 at 12:01
  • 1
    The correct way to set status bar style is to set "View controller-based status bar appearance" to YES and use preferredStatusBarStyle. When using a navigation controller one more step is needed: https://stackoverflow.com/questions/41026514/swift-viewcontroller-ignores-status-bar-style – Robin Daugherty Oct 15 '18 at 06:02

1 Answers1

45

Here is Apple Guidelines/Instruction about status bar change.

If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance to NO in your .plist file. And in your appdelegate > didFinishLaunchingWithOptions add following ine (programatically you can do it from app delegate).

Objective C

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];

Swift

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

if you want to set status bar style, at view controller level then follow these steps:

  1. Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file, if you need to set status bar style at UIViewController level only.
  2. In the viewDidLoad add function - setNeedsStatusBarAppearanceUpdate

  3. override preferredStatusBarStyle in your view controller.

Objective C

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setNeedsStatusBarAppearanceUpdate];
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

Swift

override func viewDidLoad() {
    super.viewDidLoad()
    self.setNeedsStatusBarAppearanceUpdate()
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

Set value of .plist according to status bar style setup level.

enter image description here


You can set background color for status bar during application launch or during viewDidLoad of your view controller.

extension UIApplication {

    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }

}

// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

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


or 
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
    }

}



Here is result:

enter image description here


Guntis Treulands
  • 4,764
  • 2
  • 50
  • 72
Krunal
  • 77,632
  • 48
  • 245
  • 261