Friday, November 25, 2022

clipped() Doesn’t Affect Hit Testing

Ole Begemann:

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 for clipShape(_:). It’s often a good idea to combine these modifiers with .contentShape(Rectangle()) to bring the hit testing logic in sync with the UI.

Comments RSS · Twitter

Leave a Comment