UIScrollView With AutoLayout in iOS

SHISHIR
1 min readJun 20, 2020

--

  • 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:

  1. Add UIScrollView and pin it 0,0,0,0 to superview (or your desired size)
  2. Add UIView in ScrollView, pin it 0,0,0,0 to all 4 sides and center it horizontally and vertically.
  3. In size inspector, change bottom and align center Y priority to 250. (for horizontal scroll change trailing and align center X)
  4. Add all views that you need into this view. Don’t forget to set the bottom constraint on the lowest view.
  5. Select the UIScrollView, select the size inspector and deselect Content Layout Guides.

That’s it !

--

--

SHISHIR
SHISHIR

Written by SHISHIR

{ 'designation' : 'Lead Software Engineer' , 'hobby' : [ 'Music', 'Photography', 'Travelling' ] ,’email’: ‘shishirthedev@gmail.com’ }

Responses (1)