DevPrep

All topics › Auto Layout

How does Auto Layout work with UIScrollView?

Middle PRO

The gist

UIScrollView does not compute contentSize from its subviews' intrinsic sizes — it gets it from constraints. For that the scroll view exposes two layout guides:

Rule: pin the content to all four edges of contentLayoutGuide, and lock the axis that should not scroll with a constraint to frameLayoutGuide (for a vertical list — widthAnchor). Inside the content there must be an unbroken chain of constraints from top to bottom so the height can be computed; usually a UIStackView provides it.

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