Frame vs Bounds in iOS
Problem
Explain the difference between frame and bounds on a UIView in iOS.
Be ready to discuss
frame: the view's origin and size expressed in its superview's coordinate system, i.e. where the view sits inside its parent.bounds: the view's origin and size in its own coordinate system, with origin normally (0,0) unless the content is scrolled or the coordinate space is shifted.- How applying a
transform(rotation or scale) changesframe- it becomes the smallest axis-aligned rectangle enclosing the transformed view - whileboundsstays unchanged. - Why
frameis undefined/unreliable for a non-identity transform, and why you should setbounds+centerinstead offramein that case. - Scroll views:
UIScrollViewscrolls by mutatingbounds.origin(equivalentlycontentOffset), which is why subview frames don't change as you scroll. - Setting
bounds.sizeresizes the view around itscenterwithout moving it in its own coordinate space; settingframe.sizeresizes around the origin. - Practical consequences for layout maths, hit testing, and converting points between views with
convert(_:to:).
asked …