UIScrollView has two key sizes I have to point out:
frame.size
: this is a property of UIView, which defines the UIScrollView’s box outline size.contentSize
: when content size is bigger then frame size, the UIScrollView is scrollable.Programmatically, you can set the two key sizes making a UIScrollView work well. However, if you want to use Autolayout on a UIScrollView, it’d be a little tricky.
For frame.size
, it’s just as usual as any other UIView, just give enough constrains to the UIScrollView.
For contentSize
, there must have a UIView (or UIView’s sub view) as a child of the UIScrollView, and the UIView must have enough constrains to explicitly tell the UIScrollView what contentSize it should be. Which means the UIView must have these 6 constrains (missing any one will cause a “content size ambiguous” warning):
However, just because these 6 constrains must exist doesn’t mean they must have the highest “required(1000)” priority.
For example, in one of my cases, my content grows in the vertical axis to the bottom, so that I set the Y position and bottom space constrains as “high(750)” priority. If I don’t do so, there might be constrains conflict in runtime and the UIScrollView might perform abnormally.