I have this example:
struct ContentView: View {
var body: some View {
HStack {
Text("part1")
Spacer()
Text("part2")
Spacer()
Text("part3")
}
.onTapGesture {
print("tapped")
}
}
}
The problem is, that the .onTapGesture
is only getting called, when I tap on a Text
, but not if I tap on the space between the elements.
I tried to but a Rectangle().fill(Color.clear)
as a background to the HStack
, but it didn't work either (it just works, if I put i. e. Color.blue
).
It needs to specify content shape to cover all area, because by default only opaque things handle gesture
}
.contentShape(Rectangle()) // << here !!
.onTapGesture {
print("tapped")
}
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