[uiview] UIView's frame, bounds, center, origin, when to use what?

UIView has the properties frame, bounds, center, and origin, and they all seem to be interrelated. Most of the time, I deal with frame when setting the position and size of a UIView. I understand that frame is using global coordinate system and bounds is using coordinate of the local view (therefore its x and y are 0, but not always), but it's still confusing to me when to use what.

Under what context (and what's the right time) the other properties (bounds, center, origin) should be used?

This question is related to uiview uikit frame bounds

The answer is


The properties center, bounds and frame are interlocked: changing one will update the others, so use them however you want. For example, instead of modifying the x/y params of frame to recenter a view, just update the center property.


They are related values, and kept consistent by the property setter/getter methods (and using the fact that frame is a purely synthesized value, not backed by an actual instance variable).

The main equations are:

frame.origin = center - bounds.size / 2

(which is the same as)

center = frame.origin + bounds.size / 2

(and there’s also)

frame.size = bounds.size

That's not code, just equations to express the invariant between the three properties. These equations also assume your view's transform is the identity, which it is by default. If it's not, then bounds and center keep the same meaning, but frame can change. Unless you're doing non-right-angle rotations, the frame will always be the transformed view in terms of the superview's coordinates.

This stuff is all explained in more detail with a useful mini-library here:

http://bynomial.com/blog/?p=24


Examples related to uiview

How can I mimic the bottom sheet from the Maps app? UIView touch event in controller Swift addsubview and remove it Swift UIView background color opacity How do I change UIView Size? How to add constraints programmatically using Swift Load a UIView from nib in Swift Remove all constraints affecting a UIView How do I write a custom init for a UIView subclass in Swift? How to use UIVisualEffectView to Blur Image?

Examples related to uikit

CGRectMake, CGPointMake, CGSizeMake, CGRectZero, CGPointZero is unavailable in Swift How to trap on UIViewAlertForUnsatisfiableConstraints? Changing Placeholder Text Color with Swift Move view with keyboard using Swift How to use UIVisualEffectView to Blur Image? preferredStatusBarStyle isn't called How to change Navigation Bar color in iOS 7? UICollectionView spacing margins How to programmatically get iOS status bar height Get UIScrollView to scroll to the top

Examples related to frame

Adjust UILabel height to text What is the height of Navigation Bar in iOS 7? How do I change the background of a Frame in Tkinter? Tkinter scrollbar for frame Python Tkinter clearing a frame Switch between two frames in tkinter Html code as IFRAME source rather than a URL UIView frame, bounds and center Simple way to change the position of UIView? What's the difference between IFrame and Frame?

Examples related to bounds

Error : Index was outside the bounds of the array. UIView frame, bounds and center UIView's frame, bounds, center, origin, when to use what? When is layoutSubviews called?