admin管理员组

文章数量:1287085

This question Swift: Setting StatusBar color on IOS13+ (Using statusBarManager) has a very good answer on how to set the statusbar text color to dark or light for the whole app or for a UIViewController.

But there is no answer on how to change the statusbar appearance dynamically.

This still works for me, but it is already deprecated since iOS 13:

// inside a function of the viewController
//set the status bar background
overrideUserInterfaceStyle = darkMode ? .dark : .light
// on older iOS, this is not sufficient (tested and failed on ios 15)
setNeedsStatusBarAppearanceUpdate() 
// statusBarStyle is deprecated on iOS 13+
UIApplication.shared.statusBarStyle = nightMode ? .lightContent : .darkMode

This does not work for me (the statusbar style is set when loading the view, but not updated later):

override var preferredStatusBarStyle: UIStatusBarStyle {
    return darkMode ? .lightContent : .darkContent
}

This question Swift: Setting StatusBar color on IOS13+ (Using statusBarManager) has a very good answer on how to set the statusbar text color to dark or light for the whole app or for a UIViewController.

But there is no answer on how to change the statusbar appearance dynamically.

This still works for me, but it is already deprecated since iOS 13:

// inside a function of the viewController
//set the status bar background
overrideUserInterfaceStyle = darkMode ? .dark : .light
// on older iOS, this is not sufficient (tested and failed on ios 15)
setNeedsStatusBarAppearanceUpdate() 
// statusBarStyle is deprecated on iOS 13+
UIApplication.shared.statusBarStyle = nightMode ? .lightContent : .darkMode

This does not work for me (the statusbar style is set when loading the view, but not updated later):

override var preferredStatusBarStyle: UIStatusBarStyle {
    return darkMode ? .lightContent : .darkContent
}
Share Improve this question asked Feb 25 at 7:58 Lorenz MeyerLorenz Meyer 19.9k23 gold badges82 silver badges128 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Make sure info.plist doesn't contains Appearance

Than implement code like given below in controller

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setNeedsStatusBarAppearanceUpdate()
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return UIScreen.main.traitCollection.userInterfaceStyle == .dark ? .lightContent : .darkContent
    }
}

i tried this in iOS16.0+

Check with it and let me know if you have any issue in this:)

本文标签: swiftSetting StatusBar dynamically using statusBarManagerStack Overflow