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)
}
}