Hit Testing

The interviewer asks

“How does hit testing work? Explain hitTest(_:with:) and point(inside:with:).”

How to answer

Hit testing finds the deepest view under a touch, and that view becomes the first responder for the touch. The window calls hitTest(_:with:) on itself. Each view first checks point(inside:with:), then asks its subviews in reverse order (frontmost first), converting the point into each subview's coordinate space, and returns the first non-nil result, or itself. Views that are hidden, have isUserInteractionEnabled = false, or have alpha < 0.01 are skipped along with their whole subtree.

Key points

  • point(inside:with:): is this point (in my coordinates) inside me? Default is bounds.contains(point)
  • hitTest(_:with:): returns the frontmost, deepest view that contains the point, or nil
  • Order: subviews from last to first (front to back), depth-first
  • Skipped: isHidden, isUserInteractionEnabled == false, alpha < 0.01
  • The hit view's gesture recognizers, and those of its ancestors, see the touch too, so recognizers on a superview can win over a control's touches

If they dig deeper

  • Outside the bounds: a subview that sticks out of its superview isn't hittable there, even with clipsToBounds = false, because the parent's point(inside:) fails first. Override the parent's point(inside:) or hitTest to fix it.
  • Pass-through overlays: return nil when the hit is the container itself so touches reach views behind (floating toolbars, HUDs).
  • Bigger targets: override point(inside:) to grow a small control's target to the 44×44 pt minimum without changing its layout.
  • Defaults: UILabel and UIImageView have user interaction disabled, so a tap gesture on them does nothing until you enable it.
  • Debugging: the view debugger and po view.hitTest(point, with: nil) in LLDB show what is actually on top.

Common pitfalls

  • Not calling super (you lose the hidden, alpha and interaction checks)
  • Passing a point in the wrong coordinate space (always convert(_:to:) / convert(_:from:))
  • An invisible overlay with alpha = 0.02 or a clear background still swallowing touches
  • Expanding point(inside:) beyond the superview's bounds and expecting it to work

How to use it

swift
@MainActor
final class PassThroughView: UIView {            // e.g. a floating overlay
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let hit = super.hitTest(point, with: event)
        return hit === self ? nil : hit           // only subviews catch touches
    }
}

@MainActor
final class SmallIconButton: UIButton {
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        let dx = max(0, 44 - bounds.width) / 2
        let dy = max(0, 44 - bounds.height) / 2
        return bounds.insetBy(dx: -dx, dy: -dy).contains(point)   // 44×44 pt minimum
    }
}

@MainActor
final class CardView: UIView {                   // badge hangs outside the card
    let badge = UIView()
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        if super.point(inside: point, with: event) { return true }
        let p = badge.convert(point, from: self)
        return !badge.isHidden && badge.point(inside: p, with: event)
    }
}

Best practices

  • Call super and adjust its result rather than reimplementing the walk
  • Convert points between coordinate spaces explicitly
  • Return nil from hitTest to let touches fall through to views behind
  • Prefer point(inside:) for bigger tap targets, and keep them at least 44×44 pt
  • Keep overrides cheap; hit testing runs for every touch and pointer movement

Related concepts

  • Responder chain, Gesture recognizers, UIGestureRecognizerDelegate (shouldReceive), Accessibility (target sizes), View hierarchy