Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change background color of button in SwiftUI?

Tags:

button

swiftui

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.

like image 779
wictorious Avatar asked Jun 15 '19 12:06

wictorious


People also ask

How do I change the background color of a button in SwiftUI?

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.

How do I change the button background in Xcode?

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.

How do I add custom background color in SwiftUI?

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.


2 Answers

enter image description here

Button(action: { }) {     Text("Button Text!").padding() } .background(Color.black) 
like image 173
Daniel Avatar answered Sep 19 '22 13:09

Daniel


enter image description here

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!

like image 36
Tuan Nguyen Avatar answered Sep 19 '22 13:09

Tuan Nguyen