How does Auto Layout work with UIScrollView?
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:
contentLayoutGuide— the rectangle of the scrollable content; its size becomescontentSize.frameLayoutGuide— the visible frame of the scroll view (itsboundsignoring the offset).
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