Tomáš Pánik
Tomáš Pánik

Reputation: 596

How to change status bar style - iOS 12

I need to update status bar style on every view controller based on the background color (what UINavigationController is doing automatically).

Have tried all the options described on stackoverflow (View controller-based status bar appearance in info.plist set to YES), but none worked for me.

I am using Xcode 10 beta 6 and Swift 4.2, targeting iOS 12.

Upvotes: 27

Views: 35899

Answers (6)

slushy
slushy

Reputation: 12385

Swift 4+, iOS 12+

View controller-based status bar appearance now needs to be set to YES in info.plist since UIKit no longer wants us to edit status bar style through UIApplication.shared—status bar style is now view-controller based.

Then if you want the change to be applied at app level, simply override preferredStatusBarStyle in the appropriate container view controller (ideally the root)...

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

...and this will propagate to all view controllers underneath it. And if you want to edit status bar style per view controller, apply this override per view controller.

If status bar style ever changes during runtime, then you need to call setNeedsStatusBarAppearanceUpdate() (from anywhere in the container/root view controller or that specific view controller), otherwise it isn't needed.

Upvotes: 34

Deviyani Swami
Deviyani Swami

Reputation: 767

override func viewDidLoad(){
    super.viewDidLoad()

    navigationController?.navigationBar.barStyle = .default
}

override var prefersStatusBarHidden: Bool {
    return true
}

I applied scroll view also in the same screen, by this code was able to resolve status bar issue.

Upvotes: -1

thexande
thexande

Reputation: 1750

If you're wrapped in a nav controller, you're going to need this:

final class LightNavigationController: UINavigationController {
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}

Upvotes: 2

Jim Bray
Jim Bray

Reputation: 788

I have Xcode 10.2 and found as you did that setting the View controller-based status bar appearance in info.plist to "YES" will not change the status bar style at all.

However, I did find that changing two keys in the info.plist file will do all the work of changing the status bar to light or dark without any additional coding.

Here's what I did to fix it for myself
When hovering over the top line "Information Property List" from the info.plist file, a small round "+" button will appear. Click this button and scroll through the items until you find the following keys.

View controller-based status bar appearance [set this value to] NO
Status bar style [set this value to] UIStatusBarStyleLightContent

NOTE: The UIStatusBarStyleLightContent is not found as a selectable item in the value list and must be typed into the value box.

info.plist

I hope this helps you or anyone else looking for an answer to this question.

Upvotes: 11

swearwolf
swearwolf

Reputation: 337

If you have View controller-based status bar appearance in info.plist set to YES and your view controller is embedded into UINavigationController, then your navigation controller will be responsible for updating bar style (through navigationController.navigationBar.barStyle) and preferredStatusBarStyle property will be ignored

Upvotes: 15

ielyamani
ielyamani

Reputation: 18581

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

Upvotes: 29

Related Questions