iOS Interface Development Basics--frame and bounds

Source: Internet
Author: User

Reference: http://blog.csdn.net/hherima/article/details/39501857


In the UI development of iOS, it is often necessary to locate the view. The more common concept is frame and bound, through the view of the two properties, you can arbitrarily "fiddling" with our view.

Both of these properties can define the location and size of the view, but what is the difference between the two properties and how do they relate? After data search, record as follows:

The basic concept of frame and bounds

First, frame and bounds are both a cgrect structure:

struct CGRect {  cgpoint origin;  Cgsize size;};

Origin can set the location of the view (x, y) and size to set the view (width, height).

Then the difference between frame and bounds can be discussed on origin and size, respectively.

The origin of frame and bounds

1, its coordinate system is different.

    • The origin coordinate system of a frame is its parent view, that is, its x, y coordinates are in the context of its parent view.
    • The origin coordinate system of bounds is in its own context.
Here's a quote from a note

2, frame and the origin of the bounds different role.
    • Frame sets origin to be positioned in the parent view, such as the origin of the frame where you set the Sub View (10,10), and when the child view is added to the parent view, the position of the child view is in the parent view coordinate system (10,10).
    • The origin of the bounds, which is reflected in the setting of the bounds view when adding sub-view, set the origin of the bounds, will cause the view of the Origin shift, The coordinates of the upper-left corner of the view are changed (the default view's upper-left coordinate is the view's origin coordinate (0,0)). When you set the origin of bounds for a view (suppose called Viewa) (which we assume is set to -20,-15), the coordinates of the upper-left corner of the view become ( -20,-15). When adding Subview to Viewa (assuming VIEWB), if the origin of the frame VIEWB is set to (0,0), it should be displayed in the upper-left corner of the Viewa, but at this point the upper-left coordinate of the Viewa is ( -20,-15), then this time (0 , 0) The coordinates are not in the upper left corner of the Viewa, but as shown in the place. At the same time, can compare two pictures, viewb Frame.origin are (0,0), but Viewa bounds.origin different and cause VIEWB final position is different.


Frame and the size of the bounds compared to origin,frame and bounds size difference is not so big, they are in the view itself, to set the size of the view, that is, width and height. When the size of the frame and bounds properties of the view is set to the same sizes, the two properties make no difference.But when the frame is different from the size of the bounds, the problem is: the size you set later replaces the size you set earlier. That is, you may set bounds.size = (100,200) at the beginning of the period, and then set frame.size= (30,10), then the size of the actual size of the view will be set later, that is, size is (30,10).In addition to this guideline, it is also important to note that if you set the frame and then set the bounds, then the previously set Frame.origin will be changed by the size of the view itself. The specific rule is that frame will increase (decrease) the corresponding value evenly in the left and right (upper and lower) positions. For example, the following code:
    _viewa = [[UIView alloc] init];    CGRect recta = CGRectMake (n, a, ten, a);    _viewa.frame = recta;    _viewa.bounds = CGRectMake (0, 0, +);    CGRect temp = _viewa.frame;

At this point, the value of temp is

Origin= (x=-40, y=100) size= (width=200, height=20)

The reason is x=-40, because the width of Viewa changed from 100 to 200,viewa in the left and right direction each increase 50 (total 100), then, in the x direction, its coordinates is 10-50 = -40.height from 100 to 20, The Viewa is reduced by 40 in the upper and lower directions, and the coordinates are 60+40=100 in the y direction.
To summarize, the frame property helps us to locate the child view in the parent view, while the bounds property is used primarily to set the origin coordinates of this view. As for size, the individual feel uniform size, if you set a different size in bounds and frame, then it will be a certain confusion, personal feeling is not interesting.

iOS Interface Development Basics--frame and bounds

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.