Updated
Nowadays, Apple realized the problem we solved many years ago (lol_face) and provides Content Layout Guide and Frame Layout Guide as part of the UIScrollView
. Therefore you need to go through the following steps:
-
Same as above;
-
For this
contentView
, set top, bottom, left, and right margins to 0 pinning them to the Content Layout Guide of the scroll view; -
Now set the
contentView
‘s height equal to the Frame Layout Guide‘s height. Do the same for the width; -
Finally, set the priority of the equal height constraints to 250 (if you need the view to scroll vertically, the width to scroll horizzontally).
Finished.
Now you can add all your views in that contentView
, and the contentSize
of the scrollView
will be automatically resized according with the contentView
.
Don’t forget to set the constraint from the bottom of the last object in your contentView
to the contentView
‘s margin.
Original [Deprecated]
So I just sorted out in this way:
-
Inside the
UIScrollView
add aUIView
(we can call thatcontentView
); -
In this
contentView
, set top, bottom, left and right margins to 0 (of course from thescrollView
which is thesuperView
); Set also align center horizontally and vertically;