Junior — Middle
Can you give an example of using standard components where the origin property for bounds differs from zero?
sobes.tech AI
Answer from AI
In iOS, the origin property of bounds is usually (0,0) because bounds describes the coordinates and size in the view's own coordinate system.
However, there are cases when bounds.origin differs from zero:
- When using animations that change
bounds.originto shift content without changing the view's position. - In UIScrollView,
bounds.originchanges during scrolling, reflecting the content offset inside the scroll view.
Example with UIScrollView:
let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: 300, height: 500))
scrollView.contentSize = CGSize(width: 300, height: 1000)
// When scrolling down
scrollView.contentOffset = CGPoint(x: 0, y: 100)
// Then bounds.origin.y will be 100, reflecting the content offset
print(scrollView.bounds.origin) // (0.0, 100.0)
Thus, bounds.origin differs from zero when it needs to reflect the internal content offset inside the component without changing its position on the screen.