What is the best way to change the button's background color in SwiftUI?
I want to create a tile which is a button with centered Text and rectangular background of some color.
What is the best way to change the button's background color in SwiftUI?
I want to create a tile which is a button with centered Text and rectangular background of some color.
You can add a modified directly to the button without any containers (ex: Group)
Button(action: {}) {
Text("Click me!")
}
.padding()
.foregroundColor(.white)
.background(Color.red)
Hoping to help someone!
Also in SwiftUI, if you're using the .buttonStyle and want to change the background color, you'd use .tint, not .background. For example, this is how you can change a borderedProminent button style to green:
Button("Press Me!") {
//stuff to do
}
.buttonStyle(.borderedProminent)
.tint(.green)
Button(action: {self.buttonTapped()}) {
Text("Button")
.padding(.all, 12)
.foregroundColor(.white)
.background(Color.red)
}
that's how the whole bg is clickable
SwiftUI color
From iOS v13 and above, when you work with colors you should take into account Light and Dark mode.
.foregroundColor(.red)
.background(.blue)
For example .black in dark mode will be invisible(black on black). You can use system colors as a variant. For example UIColor.label