简体   繁体   中英

How to get the light or dark mode version of a SwiftUI Color?

How do I create a local variable with specifically the dark mode color in SwiftUI. I am trying to avoid specifying the color scheme of each view to be dark to get the dark mode color.

For example in UIKit, I can get the dark mode color with this code

let traitCollection = UITraitCollection(userInterfaceStyle: .dark)
let darkModeBlueUIColor = UIColor.systemBlue.resolvedColor(with: traitCollection)

I know I can convert from a UIColor, but I want to specify it only using SwiftUI so it works on all platforms.

let darkModeBlueSwiftuiColor = Color(darkModeBlueUIColor)

I would like to do something like this involving a helper function

let darkModeBlueColor = Color.blue.darkModeColor

To draw color for specific color schema just use it in view body as

    Color("testColor")
        .colorScheme(.dark)   // << this !!

also if you use color variable then same can be done.

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