ios – SwiftUI Colour in Gentle/Darkish Mode Would not Replace in Preview or Simulator


I am engaged on a SwiftUI check undertaking that includes adjusting colors primarily based 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.

theme.colorset

Nonetheless, 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 gadget when launched in darkish mode.

I initially thought this could possibly be a caching subject, however clearing the cache hasn’t resolved it.

SwiftUI preview light and dark mode

This is a minimal instance of the code I am working with:

struct ContentView: View {
    personal var baseColor: Colour = .theme
    var physique: some View {
        LuminanceVStack(baseColor: baseColor, incrementPercentage: 5)
    }
}

struct LuminanceVStack: View {
    var baseColor: Colour
    var incrementPercentage: Double

    var physique: some View {
        VStack(spacing: 0) {
            let stepCount = Int(100.0 / incrementPercentage) + 1

            ForEach(0.. Colour {
        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 Colour(
            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 Gadget: Examined on a bodily gadget 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 modify in response to the lively mode? Any strategies for methods to troubleshoot or method this could be appreciated.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles