4

Using UIColor.init(named: "customColorSet") it is no problem to get color from ColorSet which matches the current userInterfaceStyle.

However, how can I get a specific color from the color set, e.g. the light or dark color?

I tried using UIColor.init(named: "customColorSet", in: nil, compatibleWith: UITraitCollection(userInterfaceStyle: .light)) but it doed not seem to work, because still the color matching the current userInterfaceStyle is returned (dark in my case).

Andrei Herford
  • 17,570
  • 19
  • 91
  • 225

1 Answers1

9

You can use UIColor.resolvedColor(with:)

Tested:

let color = UIColor(named: "Color")?.resolvedColor(with: UITraitCollection(userInterfaceStyle: .light))
let rgb = color!.cgColor.components
shim
  • 9,289
  • 12
  • 69
  • 108
  • 1
    This didn't work for me when initializing the `UIColor` with a SwiftUI `Color` For whatever reason I consistently get the light mode color in this situation. – Ruben Martinez Jr. Mar 23 '23 at 16:30
  • I'm trying this too with a colour that SwiftUI has loaded and it's also returning the same colour regardless of the user interface style. – drekka Apr 14 '23 at 10:22