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

Viewed 637

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
1 Answers

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.

Related