UIScrollView Autolayout

Posted on March 5, 2016

Last updated on March 5, 2016

It took me a while to figure how to get a UIScrollView working with AutoLayout so here are my notes how to do it. This is around the third time I had to do it and can never remember, so noting it down for future reference.

  1. The UIScrollView has to have only one child UIView (call it ContentView)
  2. Put all your views into ContentView
  3. Make ContentView have an Equal Width constraint with the superview of the UIScrollView
  4. Make sure constraints for the scroll view ensure it has the same width as its superview.
  5. Make sure ContentView is pinned exactly to the scroll view - all the constraints for leading, trailing, top and bottom spacing should be 0.
  6. Use Autolayout for the views in the ContentView. You have to make sure that the height is computable, for example by ensuring the very bottom element has a fixed bottom spacing to the superview margin.

Hope that was helpful.

Markdown SHA1: b16da309123b58030e97de9fe735acb766b3c4f7