What is the difference between frame and bounds in UIView?

Why Interviewers Ask This

Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for Swift & iOS development. It reveals whether you understand the building blocks that more complex concepts rely on.

Answer

frame and bounds are both CGRect properties of UIView but use different coordinate systems: frame: the view's size and position in its superview's coordinate system. The origin is relative to the superview: view.frame = CGRect(x: 20, y: 50, width: 200, height: 100) // Positioned at (20, 50) in superview. bounds: the view's size and position in its own coordinate system. The origin is usually (0, 0) unless scrolled: view.bounds // Usually CGRect(x: 0, y: 0, width: 200, height: 100). Key difference — when they diverge: If you rotate a view using a transform, the frame changes (it's the smallest bounding rectangle in superview coords) but bounds stays the same: view.transform = CGAffineTransform(rotationAngle: .pi / 4) // 45 degrees print(view.frame) // Larger bounding box around the rotated view print(view.bounds) // Still the view's own rect. UIScrollView bounds: bounds.origin changes as the user scrolls — this is how scrolling works. The contentOffset IS the bounds.origin: scrollView.bounds.origin.y == scrollView.contentOffset.y. Rules: use frame to position a view within its superview; use bounds to draw inside a view (CGContext uses bounds); use frame.size and bounds.size — they should be equal for non-transformed views; frame.origin and bounds.origin differ for transformed views. center: view.center = CGPoint(x: superview.bounds.midX, y: superview.bounds.midY) — centers in superview.

Pro Tip

This topic has Swift & iOS-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.