clipped() Doesn’t Affect Hit Testing
The
clipped()
modifier in SwiftUI clips a view to its bounds, hiding any out-of-bounds content.[…]
When you run this code, you’ll discover that the button isn’t tappable at all. This is because the (unclipped) square, despite not being fully visible, obscures the button and “steals” all taps.
[…]
The
clipped()
modifier doesn’t affect the clipped view’s hit testing region. The same is true forclipShape(_:)
. It’s often a good idea to combine these modifiers with.contentShape(Rectangle())
to bring the hit testing logic in sync with the UI.