UIScrollView with Auto Layout
I've seen a number of posts recently on blogs and SO, that either don't quite go all the way to a full explanation of how to (easily) achieve this, or make it much more difficult than it needs to be.
Our layouts need to be more flexible than ever to cope with size classes and iOS9 split-screen (& slide-over) on the iPad, so Auto Layout is an essential part of iOS development - I dread to think of the man hours lost in frustration trying to get the *#!@ing UIScrollView to scroll!
It's certainly a subject that bothered me when I first made the switch from "springs & struts", so here's a foolproof method of getting a UIScrollView to place nice with Auto Layout.
1. Add your UIScrollView to the Storyboard scene - in this example, it is directly placed inside the main view.
2. Pin the UIScrollView to the parent UIView on all sides (untick "Constrain to margins"):
3. Add exactly ONE UIView to the UIScrollView, and again pin that on all sides to its parent. Your hierarchy should look like this:
4. If you're trying to achieve a vertical (up/down) only scroll, select both the UIScrollView, and the "Content View" in the hierarchy, then set them to have "Equal Widths". If you're trying to achieve a sideways (left/right) scroll, then set "Equal Heights".
5. The critical final step is that the "Content View" must now have an "Intrinsic Content Size" (it needs to "know" how tall it should be) for the direction that you want it to scroll e.g. the direction that you didn't fix as equal to the parent in the previous step.
Therefore, in this example, we want the screen to scroll vertically - there must be a "chain" of subviews from top to bottom, they must all have top & bottom constraints, and they must all in turn have an "Intrinsic Content Size".
Labels are easy therefore, because their "Intrinsic Content Size" is set directly by the text shown in the label.
In this example therefore, the three labels each have top & bottom constraints to either the "Content View", or to each other. The chain is complete, and the Auto Layout warnings disappear - on small screens, the UIScrollView will scroll, or on screens where we can see everything without scrolling, then the scrollView won't move at all.
It's these basic principles that make it easy to get your UIScrollView to scroll (avoid the classic "scrollView won't scroll" issues), and to get rid of those nasty "Need constraints for: Y position or height" warnings in IB.