Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Text and Image to Button in SwiftUI

Tags:

ios

swiftui

I'm trying to add both Text and Image on Button like:

Button(action: {}) {
        Image("gift")
        Text("Send")
            .padding(.horizontal)
    }
    .padding()
    .foregroundColor(.white)
    .background(Color.gray)
    .cornerRadius(.infinity)

Button looks:

enter image description here

But I want to create this:

enter image description here

Thanks

like image 898
Harshal Wani Avatar asked Sep 22 '19 11:09

Harshal Wani


People also ask

How do you change a button image when the button is pressed in SwiftUI?

The button image changes depending on if it is playing or not. Then, whenever you click the button, isPlaying is changing -> if it was true it becomes false and vice versa. The button image is set by ternary operator . Save this answer.

How do I create a button action in SwiftUI?

SwiftUI's button is similar to UIButton , except it's more flexible in terms of what content it shows and it uses a closure for its action rather than the old target/action system. To create a button with a string title you would start with code like this: Button("Button title") { print("Button tapped!") }

How do I use SF symbols in SwiftUI?

To show a symbol, all we need to do is to copy the name of the symbol from the SF Symbols app and then initialize an image with it. We can then apply different configurations to the image. In SwiftUI, there are several modifiers available to configure a symbol image.


1 Answers

Just put the Image and Text inside an HStack

Button(action: {}) {
    HStack {
        Image(systemName: "gift")
        Text("Send")
    }
}
.padding()
.foregroundColor(.white)
.background(Color.gray)
.cornerRadius(.infinity)

enter image description here

like image 175
Ashley Mills Avatar answered Oct 17 '22 06:10

Ashley Mills