Search code examples
swiftcolorsstatusbar

Swift: Setting StatusBar color on IOS13+ (Using statusBarManager)


I want to define by code the color of my status bar, the way I found it was this one but it is deprecated. Does anyone know what the new way of doing this is? This warning is following me in all my codes

enter image description here

This code is working but with the warning

The code for whoever wants:

class AppDelegate: UIResponder, UIApplicationDelegate {

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

Solution

  • By using UIStatusBarManager as mention in the warning, you can not also able to set style.

    because statusBarManager is the get only property. Check Here

    open var statusBarStyle: UIStatusBarStyle { get }
    

    You have to override the preferredStatusBarStyle

    Like this

    class ViewController: UIViewController {
        
        override var preferredStatusBarStyle: UIStatusBarStyle {
            return .lightContent
        }
    }
    
    

    If you want to change the status bar style to all of your view controllers, you can set it inside the Info.plist.

    Step 1: Add View controller-based status bar appearance key and set No value

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

    Step 2: Add Status bar style key and set style like Light Content

    <key>UIStatusBarStyle</key>
        <string>UIStatusBarStyleLightContent</string>
    


    If you want a different style based on controller then, Step 1: Add View controller-based status bar appearance key and set Yes value

    <key>UIViewControllerBasedStatusBarAppearance</key>
        <true/>
    

    Step 2: override preferredStatusBarStyle within view controller.

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    

    Here is a good article about How to set status bar style.