1

How can we create a new Color programmatically with respect to the the system theme (dark/light mode). What I can do is the following, but is there a way to make it work directly inside the extension to Color?

extension UIColor {
    static let slightlyOff = UIColor { (traitCollection: UITraitCollection) -> UIColor in
        switch traitCollection.userInterfaceStyle {
        case .light: return UIColor.red
        case .dark: return UIColor.yellow
        default: return UIColor.green
        }
    }
}

extension Color{
    static let slightlyOff = Color(UIColor.slightlyOff)
}

struct ContentView: View {
    @EnvironmentObject var state:State

    var body:some View {
        Text("Dynamic Color").foregroundColor(Color.slightlyOff)
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
simibac
  • 7,672
  • 3
  • 36
  • 48
  • checkout the answer of i_priebe here: https://stackoverflow.com/questions/56531508/xcode-11-swiftuis-dark-mode-setup – Chris Feb 29 '20 at 12:51

0 Answers0