简体   繁体   中英

How to Reverse Dark & Light Content for Status Bar to Accommodate Dark Mode in Xcode

I'm trying to set up dark mode for my app but noticed that my status bar is the opposite color that it should be. When in light mode it's dark and when in dark mode it's light.

Here it is in light mode (showing wrong dark colors for status bar) 这是浅色模式(状态栏显示错误的深色)

Here it is in dark mode (showing wrong light colors for status bar)

在此处输入图片说明

Ideally I would like to reverse these status bar colors so when its in light mode the status bar is light (white) and when in dark mode the status bar is dark (black).

Any help would be highly appreciated, and thank you ahead of time!

You can set the status bar style manually if you want to have custom control over the status bar. Override preferredStatusBarStyle in your view controller

class MyViewController: UIViewController {
    override var preferredStatusBarStyle: UIStatusBarStyle {
        switch traitCollection.userInterfaceStyle { // Light Mode vs. Dark Mode
        case .light:       return .lightContent
        case .dark:        return .darkContent
        case .unspecified: return .darkContent // <-- should never happen
        }
    }
}

See userInterfaceStyle , preferredStatusBarStyle , and preferredStatusBarStyle is not working for additional details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM