ScottyBlades
ScottyBlades

Reputation: 14073

How can I programmatically access Dark mode colors when in light mode and vice versa in SwiftUI?

An accentColor can have an "Any Appearance" color and a "Dark Appearance" color. Lets say, I'm in light mode, how can I access the dark mode color programmatically without manually setting the color mode to dark mode?

Upvotes: 1

Views: 2049

Answers (1)

Asperi
Asperi

Reputation: 258443

You need to resolve your named color to trait collection of dark style, like

.background(
   Color(UIColor(named: "testColor")?.resolvedColor(with: 
      UITraitCollection(userInterfaceStyle: .dark)) ?? UIColor.darkText)
)

Tested with Xcode 12.4 / iOS 14.4

Note: tune to your color name and any default color to unwrap optional

Upvotes: 3

Related Questions