contentInset and contentOffset properties of UIScrollView

Created and inserted into controller like any other view, UIScrollView needs two additional steps to complete its configuration:

  • set the contents size property, which specifies the size of the scrollable area
  • add view or views that are displayed by scroll view

Even if we haven’t added any views to our UIScrollView yet, running NSLog(@”%@”, scrollView.subviews) will show us two UIImageViews – these are vertical and horizontal scrolling bars.

UIImageViews in UIScrollView
UIImageViews in UIScrollView

Adding one or many subviews depends on our application design, but the scroll view needs to support zooming, the most common solution is to add a single subview that encompasses the entire contentSize and then add additional subviews to that view.
The contentInset property specify a buffer area around the content of the scroll view. We use it, when we need to add padding around the edges of scroll view content.

contentInset
contentInset

It makes content area larger without changing the size of the subview or subview’s content.
To avoid confusion, let’s mention contentOffset property, which is a CGPoint value that defines the top-left corner of the scroll view bounds. It defines the point in the content that is visible at the top left of the scroll view bounds. It is used often, when we need to scroll programmatically as response to user actions (scroll to top in applications with vertical representation of the data).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s