In UIKit, how does hitTest(_:with:) choose the view that receives a touch, and which views does it pass over?
answer
- starts at the window, walks down
- point(inside:with:) is a bounds check
- the frontmost matching subview wins
- hidden, interaction off, alpha under 0.01
- UIImageView and UILabel default to no interaction
basics
~20 sUIKit's hitTest(_:with:) walks down from the window, keeping only branches whose point(inside:with:) contains the touch, and returns the deepest frontmost view; hidden views, views with interaction disabled and views with alpha below 0.01 are skipped.
solid answer
~40 sWhen a finger lands, UIKit hit-tests the window: `hitTest(_:with:)` calls `point(inside:with:)` on subviews, ignores every branch whose bounds do not contain the point, and keeps descending until it finds the **deepest, frontmost** view that does. That view becomes the touch's view for the whole sequence; gesture recognizers on it and on its ancestors see the touches first. The hit test skips views that are hidden, have `isUserInteractionEnabled` set to `false`, or have `alpha` below `0.01`, and it ignores content, so a clear overlay still catches touches. Two classic consequences: a subview sticking outside its superview's bounds gets no touches there even with `clipsToBounds` off, and a button inside a `UIImageView` or `UILabel` is dead because both default `isUserInteractionEnabled` to `false`.
code
swift · 12 linesimport UIKit
/// A full-screen overlay that draws guides over a drawing canvas
/// but lets every touch that misses its own controls fall through.
final class PassthroughOverlayView: UIView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let hit = super.hitTest(point, with: event)
// Return nil for the overlay itself so the hit test moves on
// to the views behind it; keep hits on its subviews.
return hit === self ? nil : hit
}
}go deeper
Recall the skip rules: hidden, user interaction disabled, alpha below 0.01, and points outside the view's bounds.
Walk through the search: point(inside:with:) prunes branches, the frontmost containing subview wins, and the result becomes the touch's view for its whole sequence.
Use the model to diagnose dead buttons, overlays that swallow touches and pop-out views outside their parent, and override hitTest(_:with:) or point(inside:with:) while calling super.
Treat custom hit testing as shared infrastructure: keep the overrides in a few reviewed view classes rather than scattered one-off fixes that nobody can reason about later.
## Two steps: find the view, then deliver UIKit handles a touch in two stages. First it **hit-tests**: starting from the window, it asks which view is under the finger. Then it **delivers**: the touch is bound to that view, called the hit-test view, for its whole life, from began to ended or cancelled. Gesture recognizers attached to the hit-test view or to any of its ancestors receive the touches before the view does; the view's `touchesBegan(_:with:)` family also receives them unless a recognizer delays or cancels them, and whatever the view does not handle climbs the responder chain. Everything about "why did this tap go there" is decided in the first stage, so it pays to know exactly what `hitTest(_:with:)` does. ## How hitTest(_:with:) walks the hierarchy Apple's documentation describes the walk like this: 1. The point is expressed in the receiving view's own coordinate system. 2. The method calls `point(inside:with:)` on each subview. The default `point(inside:with:)` returns `true` when the point is inside the view's **bounds**. 3. A branch whose view does not contain the point is **ignored entirely**, including every subview in it. 4. Where a view does contain the point, the search continues into its subviews, and the **frontmost** view that contains the point wins. Later entries in `subviews` are drawn on top, so they are frontmost. 5. The result is the farthest descendant that contains the point, or `nil` if the point is outside the hierarchy. ## What makes a view invisible to hit testing | Condition | Effect on hit testing | |---|---| | `isHidden == true` | the view and its subtree are skipped | | `isUserInteractionEnabled == false` | the view and its subtree are skipped | | `alpha < 0.01` | the view and its subtree are skipped | | point outside the view's bounds | the branch is skipped, even if a subview is drawn there | | clear background or transparent pixels | **no effect**: content is not considered | Two defaults surprise people: `UILabel` and `UIImageView` both declare `isUserInteractionEnabled` with a default of `false`. A button added inside an image view, such as a play badge on a thumbnail, never receives a tap until you turn interaction on for the image view. ## The traps it explains - **The invisible overlay.** A full-screen view with a clear background, laid over a drawing canvas to show guides, swallows every touch, because hit testing ignores what the view draws. - **The subview outside its parent.** A palette that pops out above a toolbar is drawn when `clipsToBounds` is `false`, but the toolbar's `point(inside:with:)` returns `false` for points outside its bounds, so the palette's branch is never searched. - **Fading out to disable.** Animating `alpha` to `0` removes a view from hit testing, while `0.02` keeps it catching touches the user cannot see. - **Disabling the parent.** Turning off `isUserInteractionEnabled` on a container silences every control inside it, which is sometimes the intent and often a bug. ## Overriding it You rarely call `hitTest(_:with:)` yourself, but overriding it, or `point(inside:with:)`, is the supported way to change routing: - Return `nil` when the hit is the view itself to let touches **pass through** an overlay while its own buttons keep working. - Override `point(inside:with:)` to widen or narrow the area a view claims. - Call `super` so the hidden, interaction and alpha rules still apply; a hand-written walk that skips them catches touches on hidden views. Keep overrides cheap and free of side effects, since they run whenever UIKit routes an event, and remember that the documentation expects the returned view to be part of a window's hierarchy. ## Where gesture recognizers fit Hit testing chooses the view, and that choice decides which recognizers are in play: those attached to the hit-test view and its ancestors. A recognizer on a sibling view never sees the touch. This is why a pan recognizer on a card and a scroll view's own pan recognizer can compete for the same finger, while a recognizer on an unrelated view stays out of it. ## Checking it in practice When a tap goes somewhere unexpected, ask UIKit directly instead of guessing: - Pause in the debugger and call `hitTest(_:with:)` on the window with the point in window coordinates and a `nil` event; the returned view is where that touch would go. - If it returns a view you did not expect, walk up its superviews and check `isHidden`, `isUserInteractionEnabled`, `alpha` and whether each ancestor's bounds really contain the point. - If it returns the right view but nothing happens, the problem has moved on to gesture recognizers or touch handling, not hit testing.
- Why does a UIButton added inside a UIImageView never respond, and what fixes it?`UIImageView` declares `isUserInteractionEnabled` with a default of `false`, and hit testing skips a view with interaction disabled together with its whole subtree, so the button is never found. Set `isUserInteractionEnabled = true` on the image view, or place the button in a sibling container instead. `UILabel` has the same default.
- Does a view with a clear background catch touches, and how do you let them through?Yes. Hit testing does not look at content, so a clear view whose bounds contain the point is a hit. If the overlay has no controls, set its `isUserInteractionEnabled` to `false`. If it has controls of its own, override `hitTest(_:with:)` to return `nil` when the result is the overlay itself, so only its subviews keep catching touches.
- A view with a clear background lets touches pass through to views behind it.
- Setting clipsToBounds to false makes the overflowing part of a subview tappable.
- UIKit picks the view whose center is closest to the finger.
- UIImageView passes taps to its subviews by default.
- A gesture recognizer on a sibling view also sees touches on the hit-test view.