Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI - Button with transparent background

Tags:

ios

swift

swiftui

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: enter image description here

How can I hide the background rectangle of the button? I want to see only the text. Touching the text should trigger the action.

like image 327
Stefan Avatar asked Dec 15 '19 14:12

Stefan


People also ask

How do you make a button transparent in Swift?

Setting the backgroundColor to clearColor makes the button transparent.

How do I make a transparent view in SwiftUI?

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.

Can I change the background color of a button using SwiftUI?

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.

How to design the button with rounded borders in SwiftUI?

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)

How to create a button using SwiftUI?

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 }

What is the difference between UIButton and button in SwiftUI?

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.


1 Answers

Thanks to Asperi to point me into the right direction.

I just had to add

.buttonStyle(PlainButtonStyle())

to the group.

like image 72
Stefan Avatar answered Nov 10 '22 21:11

Stefan