clipped() doesn’t have an effect on hit testing – Ole Begemann


The clipped() modifier in SwiftUI clips a view to its bounds, hiding any out-of-bounds content material. However word that clipping doesn’t have an effect on hit testing; the clipped view can nonetheless obtain faucets/clicks outdoors the seen space.

I examined this on iOS 16.1 and macOS 13.0.

Instance

Right here’s a 300×300 sq., which we then constrain to a 100×100 body. I additionally added a border across the outer body to visualise the views:

Rectangle()
  .fill(.orange.gradient)
  .body(width: 300, peak: 300)
  // Set view to 100×100 → renders out of bounds
  .body(width: 100, peak: 100)
  .border(.blue)

SwiftUI views don’t clip their content material by default, therefore the total 300×300 sq. stays seen. Discover the blue border that signifies the 100×100 outer body:

Now let’s add .clipped() to clip the big sq. to the 100×100 body. I additionally made the sq. tappable and added a button:

VStack {
  Button("You may't faucet me!") {
    buttonTapCount += 1
  }
  .buttonStyle(.borderedProminent)

  Rectangle()
    .fill(.orange.gradient)
    .body(width: 300, peak: 300)
    .body(width: 100, peak: 100)
    .clipped()
    .onTapGesture {
      rectTapCount += 1
    }
}

Whenever you run this code, you’ll uncover that the button isn’t tappable in any respect. It is because the (unclipped) sq., regardless of not being totally seen, obscures the button and “steals” all faucets.


Xcode preview displaying a blue button and a small orange square. A larger, partially transparent orange square covers both the smaller square and the button.
The partially clear area signifies the hit space of the orange sq.. The button isn’t tappable as a result of it’s lined by the clipped view with respect to hit testing.

The repair: .contentShape()

The contentShape(_:) modifier defines the hit testing space for a view. By including .contentShape(Rectangle()) to the 100×100 body, we restrict hit testing to that space, making the button tappable once more:

  Rectangle()
    .fill(.orange.gradient)
    .body(width: 300, peak: 300)
    .body(width: 100, peak: 100)
    .contentShape(Rectangle())
    .clipped()

Word that the order of .contentShape(Rectangle()) and .clipped() may very well be swapped. The vital factor is that contentShape is an (oblique) mother or father of the 100×100 body modifier that defines the scale of the hit testing space.

See also  App Design Apprentice | raywenderlich.com

Video demo

I made a brief video that demonstrates the impact:

  • Initially, faucets on the button, and even on the encircling whitespace, register as faucets on the sq..
  • The highest swap toggles show of the sq. earlier than clipping. This illustrates its hit testing space.
  • The second swap provides .contentShape(Rectangle()) to restrict hit testing to the seen space. Now tapping the button increments the button’s faucet rely.

The total code for this demo is accessible on GitHub.

Abstract

The clipped() modifier doesn’t have an effect on the clipped view’s hit testing area. The identical is true for clipShape(_:). It’s usually a good suggestion to mix these modifiers with .contentShape(Rectangle()) to carry the hit testing logic in sync with the UI.

Leave a Reply