11. February 2017

UIScrollView with Auto Layout Constraints

Create a single Scroll View in the View of the View Controller (the Scroll View cannot be used as View Controller view itself). Inside the Scroll View, create only a single View (hint: rename it to Content View). Inside, add the content that should be scrollable:

UIScrollView hierarchy

For both the Scroll View and the Content View create constraints using Pin that constrain the View in all directions with a distance of 0 without margin:

Pin borders of Scroll View and Content View

If the Content View has an intrinsic content size, for example an Image View with an image set, that’s all that is needed to make the content scrollable. Otherwise, the Content View needs its width and height constrained so that the size of the scrollable area can be known:

Example project

ScrollViewExample.zip

More information

iOS Training Schulung Workshop
Nächste iOS-Schulung:
21. – 25. Oktober 2019
München
iOS Buch
iOS 12-Apps entwickeln mit Swift & Xcode 10