How can I use Autolayout to set constraints on my UIScrollview?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
UIScrollView works with Auto Layout, but only if you constrain the right things to the right guides. The clean modern pattern is to pin the scroll view to its parent, place a content view inside it, pin that content view to the scroll view's contentLayoutGuide, and then constrain the content view's width or height to the frameLayoutGuide depending on the scroll direction.
The Key Mental Model
With UIScrollView, there are really two layout concerns:
- the visible frame of the scroll view
- the size of the scrollable content
Auto Layout handles those through two guides:
- '
frameLayoutGuidefor the visible viewport' - '
contentLayoutGuidefor the scrollable content area'
If you mix those up, the scroll view either will not scroll or will produce ambiguous constraints.
Vertical Scrolling Setup
For a vertically scrolling layout, the common setup is:
- constrain the scroll view to the parent view
- add a
contentViewinside the scroll view - pin
contentViewon all four sides tocontentLayoutGuide - make
contentView.widthAnchorequal toframeLayoutGuide.widthAnchor
That last width constraint is what prevents unwanted horizontal scrolling while still allowing vertical expansion.
Programmatic Example
At this point, the scroll view still needs content inside contentView whose constraints define the content height. That is what gives the scroll view something to scroll.
Content Must Define Its Own Size
The scroll view does not guess content size from nowhere. The subviews inside contentView must fully constrain the content from top to bottom.
Example with a vertical stack:
The bottom anchor is crucial. Without it, Auto Layout may not know the final content height.
Interface Builder Version
In Interface Builder, the same logic applies:
- constrain the scroll view to the parent view
- add one content view inside the scroll view
- pin the content view to the content layout guide
- equalize content view width to the frame layout guide for vertical scrolling
Older tutorials often refer to manually setting contentSize. That is not the preferred approach when using Auto Layout correctly.
Horizontal or Two-Directional Scrolling
If you want horizontal scrolling instead, constrain the content view height to the frame layout guide and let width grow from content. For two-direction scrolling, you usually omit those equal-width or equal-height constraints and instead fully define the content size in both dimensions through subview constraints.
The rule is consistent:
- lock the non-scrolling axis to the frame
- let the scrolling axis be determined by content
Common Pitfalls
- Pinning the content view to the scroll view itself instead of using
contentLayoutGuideandframeLayoutGuide. - Forgetting the equal-width constraint for vertical scrolling, which often causes unwanted horizontal scrolling or ambiguous layout.
- Building the content subviews without a bottom constraint, leaving Auto Layout unable to infer the content height.
- Mixing manual
contentSizeupdates with Auto Layout constraints, which usually creates confusion and conflicting layout logic. - Assuming the scroll view will scroll just because subviews exist inside it. The constraints must define a content area larger than the visible frame.
Summary
- Use
contentLayoutGuidefor scrollable content edges andframeLayoutGuidefor the visible viewport. - For vertical scrolling, pin the content view to the content guide and match its width to the frame guide.
- Make sure the inner content is fully constrained from top to bottom so Auto Layout can infer the content height.
- The same idea works in both code and Interface Builder.
- If the constraints correctly define the content size, you usually do not need to set
contentSizemanually.

