TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: I'm not using a view controller, how to sent style with statusBarStyle?

Forums > SwiftUI

A user can toggle dark mode via some options, and when they do, the UI updates


class Utilities: ObservableObject {

    var barStyle: UIStatusBarStyle = .darkContent

    // The default is to use the system's default.
    @AppStorage("theme") var theme: String = ""

    var userInterfaceStyle: ColorScheme? = .dark

    func overrideDisplayMode() {
        var userInterfaceStyle: UIUserInterfaceStyle
        //var barStyle: UIStatusBarStyle

        if theme == "On" {
            userInterfaceStyle = .dark
            barStyle = .lightContent
        } else if theme == "Off" {
            userInterfaceStyle = .light
            barStyle = .darkContent
        } else {
            userInterfaceStyle = .unspecified
            barStyle = .default
        }

        let scenes = UIApplication.shared.connectedScenes
        let windowScene = scenes.first as? UIWindowScene

       // Update all scenes
        windowScene?.windows.forEach({ window in
            window.overrideUserInterfaceStyle = userInterfaceStyle
        })

        UIApplication.shared.statusBarStyle = barStyle
        //UIApplication.shared.setStatusBarStyle(barStyle, animated: false)
    }

}

This works great, no problem, but Apple is give me a heads up:

'statusBarStyle' was deprecated in iOS 13.0: Use the statusBarManager property of the window scene instead.

All the answers are stating use .plist or override UIViewController. I'm not using UIViewController and with this setup, statusBarStyle should update whenever theme changes. Again, the code above works as expected. How to rewrite UIApplication.shared.statusBarStyle = barStyle to please Apple?

2      

This seems to work well:

SwiftUI Change Status Bar Color with UIWindow

Play around with that and see if it does what you want.

3      

Thank you! Works on first try!

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.