I want to create a game menu which shows a list of different options like 'New Game'.
I've realised that with buttons:
var body: some View {
VStack {
Image("Title")
Group {
Button(LocalizedStringKey("new"), action: {}
Button(LocalizedStringKey("load"), action: {})
Button(LocalizedStringKey("save"), action: {})
Button(LocalizedStringKey("resume"), action: {})
Button(LocalizedStringKey("main"), action: {})
Button(LocalizedStringKey("tutorial"), action: {})
Button(LocalizedStringKey("credits"), action: {})
}
.background(Color.clear)
.font(.custom("Snell Roundhand", size: 24))
.padding()
}
}
and it looks like this:
How can I hide the background rectangle of the button? I want to see only the text. Touching the text should trigger the action.
Setting the backgroundColor to clearColor makes the button transparent.
Any SwiftUI view can be partially or wholly transparent using the opacity() modifier. This accepts a value between 0 (completely invisible) and 1 (fully opaque), just like the alpha property of UIView in UIKit.
This needs to be on the Text and not on the Button view. If you build and run the app you should see the following: Changing the background color of a button using SwiftUI is almost the same as changing the foreground color.
What if you want to design the button with rounded borders like this? SwiftUI comes with a modifier named cornerRadius that lets you easily create rounded corners. To render the button’s background with rounded corners, you can simply use the modifier and specify the corner radius like this:.cornerRadius (40)
It’s very easy to create a button using SwiftUI. Basically, you can use the code snippet below to create a button: Button(action: { // What to perform }) { // How the button looks like }
If you have learned iOS programming before, Button in SwiftUI is very similar to UIButton in UIKit. It’s just more flexible and customizable. You will understand what I mean in a while.
Thanks to Asperi to point me into the right direction.
I just had to add
.buttonStyle(PlainButtonStyle())
to the group.
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