The problem is to make a NavigationLink from a Rectangle which:
- does not have a foreground color (or has a
Color.clearforeground color) and - has
buttonStyleset toPlainButtonStyle()or.plain(to remove the blue text overlay)
have hit testing such that gestures in the empty space trigger navigation.
Simple example:
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
// tap gestures anywhere in the rectangle work as expected
NavigationLink(destination: Text("target 1")) {
Rectangle().foregroundColor(.red)
}
.buttonStyle(PlainButtonStyle())
// touch gestures anywhere in the rectangle don't navigate (if this were a ZStack with some Text in the Rectangle, only gestures on the Text would work)
NavigationLink(destination: Text("target 2")) {
Rectangle().stroke(lineWidth: 2)
}
.buttonStyle(.plain)
}
.padding(20)
}
}
}
Preview:
The problem is that gestures inside the second (white or clear) rectangle don't trigger navigation. Removing the buttonStyle(.plain) is not an option.

The solution is to add a
contentShapemodifier to tell the system how to do hit testing for theNavigationLink:I suspect this has to do with how the hit testing is managed for the
PlainButtonStyle, as alluded to in this answer:(source)