iOS 7 and Later: Set Status Bar Style Per View Controller

iOS 7 and later: set status bar style per view controller

Have you tried this?

  1. Set "View controller-based status bar appearance" (UIViewControllerBasedStatusBarAppearance) to YES in your Info.plist. (YES is the default, so you can also just leave this value out of your plist.)

  2. In your viewDidLoad method, call [self setNeedsStatusBarAppearanceUpdate].

  3. Implement preferredStatusBarStyle, returning the status bar style that you want for this view controller.

    - (UIStatusBarStyle) preferredStatusBarStyle { 
    return UIStatusBarStyleLightContent;
    }

How to change the status bar style when a view controller is loading?

The problem you're experiencing is that UINavigationController doesn't defer the choice of status bar to its view controllers.

Instead, for a navigation controller, the status bar style can be set by adjusting the barStyle property of its navigationBar.

If it is set to a black style, then the status bar will be light style:

navigationController?.navigationBar.barStyle = .black

Note, that this will also change the color of the navigationBar, however you can still set the bar's colour to whatever you want using barTintColor:

navigationController?.navigationBar.barTintColor = .purple

If you want to make a global change, so that all instances of UINavigationController use the same status bar style (useful if you've got multiple tabs all of which use a navigation controller), then you can add an extension on UINavigationController and override the preferredStatusBarStyle property:

extension UINavigationController {
open override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}

A final option, is to defer the choice to the view controllers in the navigation controller's stack.

To do that, override the childViewControllerForStatusBarStyle property of your navigation controller extension and have it return the topViewController:

extension UINavigationController {
open override var childViewControllerForStatusBarStyle: UIViewController? {
return topViewController
}
}

In this case, you'll need to override preferredStatusBarStyle in all of your view controllers (not the optimal approach, but it's an option if you need this granular level of control on a per-child-controller basis).

All of these solutions require that your View controller-based status bar appearance key in Info.plist is set to YES.

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

Is it possible to change Status Bar color for all view controllers?

Set the style of the status bar in AppDelegate.swift:

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

return true
}

And add the following code to your Info.plist:

<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

How to change status bar style during launch on iOS 7

To your Info.plist file add this key-value pair:

UIStatusBarStyle: UIStatusBarStyleLightContent

The default (black) value is UIStatusBarStyleDefault.

You can also append ~iphone or ~ipad to the key.

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



Related Topics



Leave a reply



Submit