简体   繁体   中英

SwiftUI: How to change default background color

I'm wondering how to change the default system background color given to iOS apps, respective of dark/light mode. I know that UIColor.systemBackground can be used to read the background color, but is there a way to actually change it?

For example, when you create a blank XCode project, the canvas is, by default, white. Is there a way to change UIColor.systemBackground so that it shows something different? (Is this even possible?)

You cannot modify it directly systemBackground because it is declared as a read-only class property. But it is declared as open though, so you can do something like:

class MyColor: UIColor {

    override class var systemBackground: UIColor {
        return .systemRed
    }

}

And use it like MyColor.systemBackground .

I find this approach unnecessary though, I would just recommend creating your own utility/helper class to define your own color palette and avoid inheriting from UIColor.

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