In a grid View, here a LazyVGrid
, I understand that to make a row "tappable" one should add the .contentShape()
modifier with a Rectangle()
shape:
LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())]) {
Text("first column")
Text("second column")
// potentially many columns
}
//.overlay(content: { Rectangle().fill() })
.contentShape(Rectangle())
.onTapGesture {
print("row tapped")
}
However, while the space in the row is now tappable, clicking on the text is not recognized as a tap. The text is selectable for copying because I have .textSelection(.enabled)
on its container.
Is there a way to make the text tappable as well?
And can that be achieved while allowing it to still be selectable?
- I have found that using an
overlay
with a filled Rectangle works, but not if the fill
is Color.clear
.
- I saw an idea to
fill
with UIColor of System background, but I would rather not import UIKit.
- I saw the idea of using a fill with
opacity
so little that it was not apparent to the user. Is that really the only way to do it? (Also the text is no longer selectable.)