UIScrollView Scrollable Content Size Ambiguity

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:

  1. Same as above;

  2. For this contentView, set top, bottom, left, and right margins to 0 pinning them to the Content Layout Guide of the scroll view;

  3. Now set the contentView‘s height equal to the Frame Layout Guide‘s height. Do the same for the width;

  4. 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:

  1. Inside the UIScrollView add a UIView (we can call that contentView);

  2. In this contentView, set top, bottom, left and right margins to 0 (of course from the scrollView which is the superView); Set also align center horizontally and vertically;

Leave a Comment