February 11, 2017 · Ralf Ebert » iOS Developer Blog »

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


More information

Btn read 3c0e607615 iOS Developer Blog
Btn subscribe 930758687e Subscribe: Email · Twitter
Btn training bbbdf557d2 Next iOS training: 25. Februar - 01. März 2019, Stuttgart
Btn about 5378472193 About me · Contact