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))
}
)
}
}
}
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.
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.
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)
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.
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:
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)
}
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