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.
Change button text color in SwiftUI To change a button's text color in SwiftUI we need to use . foregroundColor on our text view. In the above code it is almost the same as the simple button, but we used a modifier to change the foregroundColor to green. This needs to be on the Text and not on the Button view.
Just scroll a little bit in Attribute Inspector , you can find Background property in view section that will allow you set the backgroundColor for the UIButton . If you want to set backgroundColor of button programatically.
You need to create a new color by right-clicking anywhere in the list to open a menu, just click Color Set and you name the color however you want.
Button(action: { }) { Text("Button Text!").padding() } .background(Color.black)
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!
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