What is the use of the contentOffset
property in UIScrollView
?
The content size of a scroll view doesn't change anything about the bounds of a scroll view and therefore does not impact how a scroll view composites its subviews. Instead, the content size defines the scrollable area. By default, a scroll view's content size is a big, fat {w:0, h:0} .
→ UIScrollView has 3 main properties, known as contentSize, contentOffset, and contentInset.
Most views have an intrinsic content size, which refers to the amount of space the view needs for its content to appear in an ideal state. For example, the intrinsic content size of a UILabel will be the size of the text it contains using whatever font you have configured it to use.
Discussion. Use this property to extend the space between your content and the edges of the content view. The unit of size is points. The default value is UIEdgeInsetsZero . By default, UIKit automatically adjusts the content inset to account for overlapping bars.
It could be considered as the coordinate of the origin of scrollView
's frame relative to the origin of its contentView
's frame. See the picture below:
According to the documentation, the contentOffset
property represents:
The point at which the origin of the content view is offset from the origin of the scroll view.
In plain speak, it's how far the view has moved in each direction (vertical and horizontal). You can unpack vertical and horizontal distance by accessing the x
and y
properties of the CGPoint
:
CGFloat xOffset = _myScrollView.contentOffset.x; CGFloat yOffset = _myScrollView.contentOffset.y;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With