Recent Posts

Using UIScrollView with Autolayout?

Trouble with UIScrollView and constraints can be solved with 4 key things. Autolayout does work differently with UIScrollView, so it’s easy to feel like you have have a jumbled heap of constraints that don’t give you what you want.

There are a lot of awful examples and ‘solutions’ out there, but these keys make it straightfoward to create with Interface Builder (or in code), requiring no additional work or tricks:

  1. Your scrollView should have one, and only one subview. For clarity I usually name it ‘Content View’

  2. Bind your Content View to the scrollView on all four sides.

  3. If you are scrolling vertically, make the Content View equal width to the scrollView.

  4. Constrain the bottom of the Content View to the last subview. (Not the other way around)

That’s it! Your UIScrollView is all set up, and content can flex even if you’re changing it after the view appears.

 

Add your email for more useful iOS bits, delivered straight to your inbox!

* indicates required