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:
But I want to create this:
Thanks
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.
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!") }
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.
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)
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