The difference between frame and bounds of IOS view, setbounds use (delve into)

Source: Internet
Author: User
Tags joins

Foreword: In the development of iOS often encounter two words frame and bounds, this article mainly elaborated frame and bound difference, especially bound very around, difficult to understand.
First, take a look at the accepted information: first see the following code you must understand some:
-(CGRect) frame{    return CGRectMake (Self.frame.origin.x,self.frame.origin.y,self.frame.size.width, Self.frame.size.height);} -(CGRect) bounds{    return CGRectMake (0,0,self.frame.size.width,self.frame.size.height);}
Obviously, the origin of the bounds is (0,0) point (which is the coordinate system of the view itself, which is always 0, 0 points, unless the SetBounds function is called), and the frame's origin is arbitrary (relative to the coordinate position in the parent view).

Second, take a look at the pictures in the Stanford iOS tutorial video


Translate as follows:

Frame: The position and size of the view in the parent view coordinate system. (reference point is, Father's coordinate system)
Bounds: The position and size of the view in the local coordinate system. (The reference point is that the local coordinate system is equivalent to VIEWB's own coordinate system, starting at 0, 0 points)
Center: The position and size of the view's central point in the parent view coordinate system. (reference point is, Father's coordinate system)


Three, the following to explain the difference between frame and bound

Frame is easy to understand: frame (FRAME.ORIGIN.X,FRAME.ORIGIN.Y) is the offset from the parent coordinate system.

Bounds a little puzzled, slightly inattentive, think more, will go around. Each view has a local coordinate system. This coordinate system function is more important, such as the touch of the callback function in the Uitouch inside the > Coordinate values are reference to the local coordinate system coordinates. Of course bounds this attribute is also referred to this local coordinate system.

In fact, the key of the local coordinate system is to know where its origin (0,0) is located (this position is relative to the upper view of the local coordinate system, the top view is the window its local coordinate system origin is the upper left corner of the screen).

The local coordinate system's origin location can be modified by modifying the view's Bounds property.

So, bounds has such a feature:

It is reference to its own coordinate system, It can modify the origin of its own coordinate system and then think of " display location of child view .


Iv. Demonstration of Demo

    UIView *view1 = [[UIView alloc] Initwithframe:cgrectmake (+, A, ())];    [View1 Setbounds:cgrectmake ( -30, -30, $)];    View1.backgroundcolor = [Uicolor redcolor];    [Self.view addsubview:view1];//added to Self.view    NSLog (@ "View1 frame:%@========view1 bounds:%@", Nsstringfromcgrect (View1.frame), Nsstringfromcgrect (View1.bounds));        UIView *view2 = [[UIView alloc] Initwithframe:cgrectmake (0, 0, +)];    View2.backgroundcolor = [Uicolor yellowcolor];    [View1 addsubview:view2];//added to View1, [at which point the upper left corner of the View1 coordinate system is ( -30,-30)]    NSLog (@ "View2 frame:%@========view2 bounds:% @ ", Nsstringfromcgrect (View2.frame), Nsstringfromcgrect (view2.bounds));

There's nothing special about this piece of code. View1 joins the view, View2 joins the View1. In the second line of the code, the View1 is setbounds set. Comments and the effect of opening this line of code


[view1 setbounds : cgrectmake (- 30 ,- 30 , 200 , 200 )];

This line of code plays: let the view2 position change the role. Why is the ( -30,-30) offset, which allows VIEW2 to move to the lower-right corner?

This is because the SetBounds function is to force the upper-left corner of its own (view1) coordinate system to be changed to ( -30,-30). Then the origin of the view1 is naturally shifted to the lower right (30,30).


The above code console output is as follows:


(log output logs indicate that each new view default bounds is actually (0,0), and the width and height of the bounds are consistent with the frame)

It's not over yet.

Both the view and the bounds are the same size in the above code. What if view frame and bounds are not the same size?

On the above code snippet, change the view1 bounds to big! For example:[View1SetBounds:CGRectMake(- -, - -,  -,  -)];


Log shows: The View1 frame has been modified. This is because of the setbounds problem.

frame defines a frame (container) relative to the parent view, and bounds is the true display area. If the bounds is smaller than frame, it can be placed in a frame (container). If bounds is bigger than frame, it feels like frame is "big" . Frame has become {{25, 25}, {250, 250}}. How did 25 come about? Bounds is longer than frame, width is 50 pixels wide, then four sides balance, each overflow "25" pixels. :


V. Conclusion

Bounds has the following two features:

1. It can modify the origin position of its coordinate system, and then think of the "sub view" display position . This function is more like the meaning of moving the origin point.

2. Bounds, it can change the frame. If bounds is bigger than frame. Then frame will also grow larger. This function is more like the meaning of boundaries and size.

It can be speculated that the first feature of Setbound can be used for view sliding, gesture action. How does the second feature work? Find a case from the Web: You can stretch the middle cell:



The code follows, overriding the cell's Layoutsubviews method:

mycustomuitableviewcell.h-(void) layoutsubviews{    self.bounds = CGRectMake (self.bounds.origin.x,                             SELF.BOUNDS.ORIGIN.Y,                             self.bounds.size.width-50,                             self.bounds.size.height);    [Super Layoutsubviews];}

List of referenced articles:

1. CSDN-Monk307

2. Natasha the Robot


The difference between frame and bounds of IOS view, setbounds use (delve into)

Related Article

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.