UIView's frame, bounds, center, origin, when to use what?
Asked Answered
E

3

196

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?

Equestrienne answered 1/7, 2009 at 20:10 Comment(2)
The link below has the best answer. #5361869Anchovy
Frame vs Bounds with pictures: https://mcmap.net/q/23424/-cocoa-what-39-s-the-difference-between-the-frame-and-the-boundsMellisamellisent
C
241

Marco's answer above is correct, but just to expand on the question of "under what context"...

frame - this is the property you most often use for normal iPhone applications. most controls will be laid out relative to the "containing" control so the frame.origin will directly correspond to where the control needs to display, and frame.size will determine how big to make the control.

center - this is the property you will likely focus on for sprite based games and animations where movement or scaling may occur. By default animation and rotation will be based on the center of the UIView. It rarely makes sense to try and manage such objects by the frame property.

bounds - this property is not a positioning property, but defines the drawable area of the UIView "relative" to the frame. By default this property is usually (0, 0, width, height). Changing this property will allow you to draw outside of the frame or restrict drawing to a smaller area within the frame. A good discussion of this can be found at the link below. It is uncommon for this property to be manipulated unless there is specific need to adjust the drawing region. The only exception is that most programs will use the [[UIScreen mainScreen] bounds] on startup to determine the visible area for the application and setup their initial UIView's frame accordingly.

Why is there an frame rectangle and an bounds rectangle in an UIView?

Hopefully this helps clarify the circumstances where each property might get used.

Coactive answered 8/7, 2009 at 10:2 Comment(1)
I think it's worth adding that the bounds are commonly used when a view has a transform applied to it. Under these circumstances the frame property is undefined. As pointed out by other answers, modifying the center and bounds are equivalent to modifying the view's position and size respectively.Atherton
J
36

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

Josephina answered 15/4, 2010 at 0:35 Comment(0)
C
19

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.

Crystlecs answered 1/7, 2009 at 20:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.