What exactly are contentOffset, ContentInset and ContentSize of a UIScrollView?
UIScrollView
is one of the most versatile and useful controls in iOS, and has been around since iOS 2.0. It’s the basis for the very popular UITableView
and it’s a great way to present content that’s larger to fit in a single screen view.
UIScrollView
has a lot of instance properties, but contentInset
, contentOffset
, and contentSize
are probably the most frequently used. One can do wonders with a complete understanding of these properties—starting with a stretchable toolbar to pinch in pinch out of images and a lot of other cool stuff.
contentOffset
contentOffset
is the point at which the origin of the content view is offset from the origin of the scroll view. In other words, it is where the user has currently scrolled within the scroll view. This obviously changes as the user scrolls.
contentOffset
indicates the current position of the scroll view content, relative to the origin coordinates on the top-left corner. The content size remains the same, but the content offset changes to respond to user interaction.
contentInset
The contentInset
is how much the content is visually inset inside the scroll view. It is the custom distance that the content view is inset from the safe area or scroll view edges.
contentInset
allows you to specify margins or padding around the content in the scrollview. You can specify the margin programmatically as follows:
contentSize
contentSize
defines how big the scrollable content is. It is the size of the content within the UIScrollView
and how long it can be within the UIScrollView
.
The scroll view must know the size of the content-view so it knows when to stop scrolling. its default value is zero, and it must be set to use any scroll view, even if the content-size is smaller than the scroll view’s own size.
Putting it all together
Some of the popular UIComponents in iOS i.e. UITextView
, UITableView
& UICollectionView
are inherited from the UIScrollView
class, so all of these properties are applicable to UITextView
, UITableView
& UICollectionView
as well.
Thanks for reading!