struct ContentView: View {
@State private var isRed = false
var body: some View {
Button("Click Me") {
isRed.toggle()
}
.foregroundColor(isRed ? .red : /* some code */)
}
}
Is there a way to apply the following logic:
If isRed
is true
then apply the color red, else revert back to the default value for this property or the inherited value if there is one.
In other words are there keywords in SwiftUI similar to initial
and inherit
in CSS?
As already mentioned in the comments many APIs accept nil
for default / no change
struct ContentView: View {
@State private var isRed = false
var body: some View {
Button("Click Me") {
isRed.toggle()
}
.foregroundColor(isRed ? .red : nil)
}
}
This works even if you specify a different tint color
struct ContentView: View {
@State private var isRed = false
var body: some View {
Button("Click Me") {
isRed.toggle()
}
.foregroundColor(isRed ? .red : nil)
.tint(.yellow)
}
}
The default color for the buttons is the "accent color". You can use it in the modifier.
struct ContentView: View {
@State private var isRed = false
Button("Click Me") {
isRed.toggle()
}
.foregroundColor(isRed ? .red : .accentColor)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With