Objective:
In iOS development often encounter two words frame and bounds, this article mainly elaborated frame and bound difference, especially bound very around, more difficult to understand.
First, take a look at the accepted information:
See the following code first you must understand something:
-(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. (The reference point is the father's coordinate system) bounds: The view's position and size in the local coordinate system. (The reference point is that the local coordinate system is equivalent to VIEWB own coordinate system, starting at 0, 0 points) Center : The position and size of the center point of the view 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 to the local coordinate system is to know where the origin (0,0) is located (this position is also relative to the upper view of the local coordinate system, the top view is 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 refers to its own coordinate system, which modifies the origin of its own coordinate system, which in turn affects where the "child View" is displayed.
Four, use demo demonstration
[OBJC] View plain copy
View code slices to my Code slice
UIView *view1 = [[UIView alloc] Initwithframe:cgrectmake ( -, -, $, $)]; [View1 Setbounds:cgrectmake (- -, - -, $, $)]; View1.backgroundcolor=[Uicolor Redcolor]; [Self.view Addsubview:view1];//Add to Self.viewNSLog (@"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];//add to View1, [the upper-left corner of the View1 coordinate system starts at ( -30,-30)]NSLog (@"view2 frame:%@========view2 bounds:%@", Nsstringfromcgrect (View2.frame), Nsstringfromcgrect (view2.bounds));
这段代码没什么特别的地方。view1加入view中,view2加入view1中。代码第二行,对view1进行了setBounds设置。注释和打开这行代码的效果
[View1 setbounds:cgrectmake (---+,--)];
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: [View1 setbounds:cgrectmake (-30,-30, 250, 250)];
log显示:view1的frame已经被修改了。这是因为setBounds的问题。:
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 characteristics, can be analyzed from the bounds itself: bounds is a rectangle, the first part is the second half of the point is the size of the view. These two attributes also indicate two features:
Bounds are like floating on a frame. Frame is a frame, bounds is the thing that shows the sub view, the following summarizes the two characteristics of bounds:
First, for bound point: it can not change the frame's origin, changing the origin of the bounds itself, thus affecting the "sub-view" display position. This function is more like the meaning of moving bounds origin.
Second, for bound size: It can change the frame. If the size of the bounds is larger than the size of frame. Then frame will also become larger, then the origin of frame will also change. This function is more like the meaning of the boundary.
It can be speculated that the first feature of the Setbound can be used for the view slide, gesture action, because it can affect the display position of the child view.
The Contentinset value of the Tablewview is often modified in the project. This technique is often used on both sides of the screen to "stay white" up and down the head. Modify Contentinset when actually modified is also bounds.
For example, Self.tableView.contentInset = Uiedgeinsetsmake (3.5, 0, 0, 0), then the bounds of TableView has changed, and the value of Y has become-3.5. Contentinset Learn More
For example, I do several setbound functions over a period of time:
[View1 Setbounds:cgrectmake (-0,0, $, $)]; [View1 Setbounds:cgrectmake (-Ten,0, $, $)]; [View1 Setbounds:cgrectmake (- -,0, $, $)]; [View1 Setbounds:cgrectmake (- -,0, $, $)]; [View1 Setbounds:cgrectmake (- +,0, $, $)]; [View1 Setbounds:cgrectmake (- -,0, $, $)]; [View1 Setbounds:cgrectmake (- -,0, $, $)]; [View1 Setbounds:cgrectmake (- -,0, $, $)]; [View1 Setbounds:cgrectmake (- the,0, $, $)]; [View1 Setbounds:cgrectmake (- -,0, $, $)]; [View1 Setbounds:cgrectmake (- -,0, $, $)];
效果如下:
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 { = CGRectMake (self.bounds.origin.x, SELF.BOUNDS.ORIGIN.Y, self.bounds.size.height); [Super Layoutsubviews]; }
The difference between frame and bounds of IOS view, setbounds use (delve into)