UIKitMidOpen-ended
What is the difference between `frame` and `bounds`?
Explanation & Code
Answer:
frame— position and size in the parent view's coordinate systembounds— position and size in the view's own coordinate system (origin is usually0,0)
Code Example:
let parent = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
let child = UIView(frame: CGRect(x: 50, y: 100, width: 100, height: 50))
parent.addSubview(child)
print(child.frame) // (50, 100, 100, 50) — position relative to parent
print(child.bounds) // (0, 0, 100, 50) — always starts at 0,0
// When to use each:
// frame — positioning a view within its parent
// bounds — drawing inside a view (custom draw, scroll offset)
// ScrollView example — bounds.origin changes as you scroll
scrollView.contentOffset = CGPoint(x: 0, y: 200)
print(scrollView.bounds.origin) // (0, 200) — scrolled 200pt down
Rate your understanding: