Eric Roller's blog

Tuesday, September 3, 2013

UIScrollView vs. AutoLayout

Setting up a scroll view with multiple subviews (one for each page), I found myself unable to scroll. Everything looked fine. I even had the AutoLayout constraints defined for each of the subviews: pinned width, pinned height, top 0 pt, left x pt (depending on which page).

It took several debugging sessions to work out that the contentSize of the scroll view had been set (through AutoLayout) (after viewDidAppear:) to the size of a single page. Despite my constraints.

The fix is not obvious: there must be constraints for both the top-left corner as well as the bottom-right corner. Once I added an additional constraint for the last view to the right side, the contentSize was correctly calculated to contain all pages.

This tip came from a comment on StackOverflow.