I created the following UI:
struct ContentView: View {
var body: some View {
List {
Section("Section One") {
Text("Item 1")
}
.onTapGesture {
print("Section 1 was tapped")
}
Section("Section Two") {
Text("Item 2")
}
.onTapGesture {
print("Section 2 was tapped")
}
Section("Section Three") {
Text("Item 3")
}
.onTapGesture {
print("Section 3 was tapped")
}
}
}
}
This is, basically, a menu. Once an item or a section is tapped, I want something to happen. The problem is: only the text of the item and of the section header are sensitive to taps. I highlighted the background with a color to verify the tappable area:
Section("Section One") {
Text("Item 1")
}
.background(Color.red)
.onTapGesture {
print("Section 1 was tapped")
}
I then tried to stretch it like so:
Section("Section One") {
Text("Item 1")
}
.frame(maxWidth: .infinity, alignment: .leading)
.background(Color.red)
.onTapGesture {
print("Section 1 was tapped")
}
Which did work, but only until I had some solid color for the background. Once I removed the background or made the color 'clear', the tap-sensitive area shrinked again to the texts. Unfortunately, in the real app I can't use any solid colors here, as the background of the whole view is a complex gradient, I don't want to mess it up.
Is there a way to stretch the tappable area without introducing unneeded colors?