What exactly are contentOffset& contentInset of ScrollView

contentOffset indicates the current position of the scroll view content, relative to the origin coordinates on the top-left corner. You shouldn't programmatically set this value unless you would like to programmatically adjust the scroll position.

contentInset allows to specify margins around the content in the scrollview. You can specify the margin programmatically as follows:

scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 7.0)

A few observations:

  • The contentOffset is where the user has currently scrolled within the scroll view. This obviously changes as the user scrolls. You don't generally change this programmatically (unless you want to programmatically scroll somewhere, e.g. have a button to scroll to the top).

  • The contentInset is how much the content is visually inset inside the scroll view (i.e. what the "margins" within the scrollview are). You generally set this once in IB or in viewDidLoad, as the scroll view is instantiated.

  • The contentSize is how big the scrollable content is. Note, with autolayout, you don't have to specify this manually, but rather is calculated by the constraints that you specified between the scroll view and its subviews (and the constraints for the subviews and between the subviews).

To get scrolling to work correctly, it is a combination of (a) the bounds of the scroll view, less any contentInset; and (b) the contentSize, as calculated for you from the constraints of the subviews.

Tags:

Ios

Swift