- Put a scroll view into the view controller and set constraints (leading, top, trailing, bottom) as (0, 0, 0, 0)
- Put a view(contentView) inside the scroll view and set constraints (leading, top, trailing, bottom) as (0, 0, 0, 0) with scroll view.
- Our content view must have equal width and equal height with parent view (scroll view). *This is the most important step*.
- Select height constraint of content view and set priority with low(250).
- Now change the height of content view as much as you need and start to design.
- * You can also change the simulator size to see preview and work properly on storyboards.
For XCode 11+
The simplest way using autolayout
:
- Add
UIScrollView
and pin it0,0,0,0
to superview (or your desired size) - Add
UIView
in ScrollView, pin it0,0,0,0
to all 4 sides and center ithorizontally
andvertically
. - In size inspector, change
bottom
andalign center Y
priority to 250. (for horizontal scroll changetrailing
andalign center X
) - Add all views that you need into this view. Don’t forget to set the bottom constraint on the lowest view.
- Select the
UIScrollView
, select the size inspector and deselectContent Layout Guides
.
That’s it !