Difference between frame, bounds, and center
Coordinate systems and transform effects.
frame and center are in the superview's space; bounds is the view's own space, usually origin zero. A non-identity transform makes frame undefined to read.
WHAT THIS TESTS This verifies you understand UIKit's three layout properties operate in different coordinate spaces, and the often-missed rule that frame becomes unreliable once a transform is applied.
A GOOD ANSWER COVERS frame is a CGRect giving the view's position and size expressed in its superview's coordinate system. center is a CGPoint at the middle of the frame, also in the superview's space, and is convenient for moving a view. bounds is a CGRect in the view's own internal coordinate system; its origin is usually (0,0) and its size equals the frame size when the transform is identity. Changing bounds.origin scrolls the content; changing bounds.size resizes around the center. The relationship frame.size equals bounds.size holds only while transform is identity.
COMMON WRONG ANSWERS Saying bounds and frame are always equal, or that bounds origin is always zero, ignoring scroll views. Treating center as separate from frame when center is derived from it. Believing you can freely set frame after rotating a view.
LIKELY FOLLOW-UPS What Apple's documentation warns: if the transform is not the identity transform, the value of the frame property is undefined and should not be modified; use bounds and center instead. How a 2x scale leaves bounds unchanged but doubles the frame size while keeping center fixed. How rotation produces a frame that is the bounding box of the rotated view.
ONE CONCRETE EXAMPLE A 100 by 100 view at center (200, 200) has frame (150, 150, 100, 100) and bounds (0, 0, 100, 100). Apply view.transform = CGAffineTransform(rotationAngle: .pi/4). Now bounds is still (0, 0, 100, 100) and center is still (200, 200), but frame becomes roughly (129, 129, 141, 141) because it is the axis-aligned bounding box of the rotated square. To move the view you adjust center, never frame, while the transform is active.
Read the original → developer.apple.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.