DevPrep

All topics › UIKit

What does hitTest do?

Middle PRO

The gist

hitTest(_:with:) answers the question "which view should receive a touch at this point". UIKit calls it on the UIWindow, and the method recurses down the tree: for each view point(inside:with:) is checked, then its subviews are visited in reverse order (the last one added — the topmost on screen — is checked first). The deepest view containing the point is returned; it becomes the first responder for the touch.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store