Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI: How can I increase the area in which a button can be triggered?

Tags:

ios

swift

swiftui

How can I increase the area in which a button can be triggered without changing the UI?

This is my code

struct ContentView: View {
    var body: some View {
        NavigationView {
            Text("Text")

            .navigationBarTitle(Text("Title"))
            .navigationBarItems(
                leading:
                Button(action: { print("add") }) {
                    Image(systemName: SFSymbolName.plus)
                        .font(.system(size: 18))
                }
            )
        }
    }
}
like image 606
Mattis Schulte Avatar asked Nov 02 '19 22:11

Mattis Schulte


People also ask

Is it possible to customize the button in SwiftUI?

The abstractions on SwiftUI are so composable that customising the UI it's basically an intrinsic property of the framework. Let's take a quick look at some things you can do with Button. As per documentation a Button is "A control that performs an action when triggered.. That's it.

How to add gradient background to buttons in SwiftUI?

With SwiftUI, you can easily style the button with gradient background. Not only can you a specific color to the background modifier, you can easily apply a gradient effect for any buttons. All you need to do is to replace the following line of code: With: The SwiftUI framework comes with several built-in gradient effect.

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)

What is adaptive view in SwiftUI?

Keep in mind that the button will adapt to its context, this is a really powerful feature of SwiftUI: Adaptive Views. For example when a button is inside a List it will change is look to behave like a cell. But for this post let's look at buttons that are treated as buttons.


2 Answers

For this particular situation, You can add padding to all edges excluding the leading edge to the label of the button:

Button(action: { print("add") }) {
    Text("+")
        .padding(EdgeInsets(top: 20, leading: 0, bottom: 20, trailing: 50))
}
.background(Color.red) // This is just for seeing the hit area. You should get rid of it

Note that The maximum tappable area should be inside the rectangle above the title:

enter image description here

like image 55
Mojtaba Hosseini Avatar answered Oct 17 '22 00:10

Mojtaba Hosseini


The Button is as big as it's content. In your case, it is as big as the image. Your button, or the tappable area, is small because the image is just small.

You can contain your image in a bigger frame, like this:

Button(action: { print("add") }) {
    Image(systemName: SFSymbolName.plus)
        .font(.system(size: 18))
        .frame(width: 40, height: 40)
}
like image 1
samwize Avatar answered Oct 16 '22 22:10

samwize