6.1 C
New York
Friday, November 29, 2024

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 observe 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 massive sq. to the 100×100 body. I additionally made the sq. tappable and added a button:

VStack {
  Button("You'll be able to'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
    }
}

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


Xcode preview displaying a blue button and a small orange square. A larger dashed orange outline covers both the smaller square and the button.
The dashed define signifies the hit space of the orange sq.. The button isn’t tappable as a result of it’s coated 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()

Notice that the order of .contentShape(Rectangle()) and .clipped() could possibly be swapped. The essential factor is that contentShape is an (oblique) mum or dad of the 100×100 body modifier that defines the dimensions of the hit testing space.

Video demo

I made a brief video that demonstrates the impact:

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

The complete code for this demo is out there 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.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles