I am engaged on a SwiftUI take a look at mission that includes adjusting colors based mostly on gentle and darkish mode settings.
In my asset catalog, I’ve configured theme.colorset
to help totally different colors for gentle and darkish environments, permitting me to check how colors adapt to every mode.
Nevertheless, I am experiencing a problem the place my code appears to solely choose up the .gentle
mode color, whatever the setting. This downside happens within the preview canvas, simulator, and even on a bodily machine when launched in darkish mode.
I initially thought this could possibly be a caching subject, however clearing the cache hasn’t resolved it.
Here is a minimal instance of the code I am working with:
struct ContentView: View {
non-public var baseColor: Shade = .theme
var physique: some View {
LuminanceVStack(baseColor: baseColor, incrementPercentage: 5)
}
}
struct LuminanceVStack: View {
var baseColor: Shade
var incrementPercentage: Double
var physique: some View {
VStack(spacing: 0) {
let stepCount = Int(100.0 / incrementPercentage) + 1
ForEach(0.. Shade {
let uiColor = UIColor(baseColor)
var hue: CGFloat = 0
var saturation: CGFloat = 0
var brightness: CGFloat = 0
var opacity: CGFloat = 0
uiColor.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &opacity)
return Shade(
uiColor: UIColor(
hue: hue,
saturation: saturation,
brightness: max(min(luminance, 1.0), 0.0),
alpha: opacity
)
)
}
}
Demo video of simulator altering setting however color not updating
What I’ve Tried
- Preview Reset: Tried resetting the canvas preview, pondering it is perhaps a caching subject.
- Simulator: Examined within the simulator by explicitly setting the setting to darkish mode, however nonetheless solely the sunshine mode colors seem.
- Bodily Machine: Examined on a bodily machine in darkish mode, and it nonetheless solely applies the sunshine mode color.
Query
Why does the setting’s gentle/darkish setting appear to be ignored, and is there a approach to make sure the colors alter in line with the energetic mode? Any ideas for how you can troubleshoot or strategy this might be appreciated.