Hit-testing and the responder chain

UIKit Mid-level Swift 5.0+ iOS 17+ Reviewed By Alok Singh

Predict the output

A label with isUserInteractionEnabled = false sits inside a custom view that overrides touchesBegan. The user taps the label. Which view gets touchesBegan?

final class CardView: UIView {
    let label = UILabel()   // isUserInteractionEnabled is false by default
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("card touched")
    }
}
Options
Hint 1 of 1

Which views does hit-testing skip?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: B The CardView

Correct. The label is skipped, so the card is the deepest eligible view.

Why not the others?
  • A The label. Hit-testing skips views with user interaction disabled.
  • C Only the window. The card is eligible and deeper than the window.
  • D Nobody. The card has interaction enabled and handles the touch.

30-second answer

For a touch, UIKit hit-tests from the window down, choosing the deepest view under the finger that is visible, has user interaction enabled and alpha above 0.01. That view becomes the first responder for the touch. Events and nil-targeted actions then travel up the responder chain: view, superview, view controller, window, application.

Practise saying it · 45 s. No recording, just a timer

Explain hit-testing and the responder chain.

Say your answer out loud, then check which of these points you covered:

  • Deepest eligible view
  • Interaction, visibility, alpha rules
  • Events travel up the chain

Deep dive

Override hitTest(_:with:) or point(inside:with:) to enlarge tap areas.

Views outside their superview's bounds don't receive touches by default.

Keyboard input goes to the first responder (becomeFirstResponder).

Menu commands (UICommand) use the chain to find a handler.

Common mistakes

  • Forgetting that a disabled or hidden ancestor blocks all its subviews.
  • Expecting touches on a subview that extends outside its parent.

Senior take & follow-ups

Seniors explain hit-testing rules precisely and know how to extend tap areas.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you make a small button easier to tap?

    Show hint

    Override point(inside:with:) to expand the area.

  2. Why doesn't a button outside its superview's bounds respond?

    Show hint

    Hit-testing checks the superview's bounds first.

Suggest a fix if something here is wrong or out of date.