# Difference between Frame and bounds in iOS View, setbounds usage (in-depth exploration), boundssetbounds

Source: Internet
Author: User

Difference between Frame and bounds in iOS View, setbounds usage (in-depth exploration), boundssetbounds

In ios development, there are often two words Frame and bounds. This article mainly describes the differences between Frame and bound, Especially bound, which is difficult to understand.
I. First, let's take a look at the accepted materials. First, you will understand the following code:
`-(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 bounds origin is (0, 0) (that is, the coordinate system of the view, which is always by default, unless the setbounds function is called ), the origin of the frame is arbitrary (relative to the Coordinate Position in the parent view ).

2. Take a look at the pictures in the Stanford iOS tutorial video.

Translation:

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 position and size of the view in the local coordinate system. (The reference point is that the local coordinate system is equivalent to the coordinate system of ViewB, starting from)
Center: the position and size of the center of the view in the coordinate system of the parent view. (The reference point is the father's coordinate system)

Iii. Differences between frame and bound

Frame is easy to understand: frame (frame. origin. x, frame. origin. y) is the offset relative to the parent coordinate system.

Bounds is a bit confusing. If you think too much about it, it will go around. Each view has a local coordinate system. This coordinate system plays an important role. For example, the> coordinate values in the UITouch In the touch callback function refer to the coordinates of the local coordinate system. Of course, the bounds attribute is also based on the local coordinate system.

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

You can modify the bounds attribute of the view to modify the origin position of the local coordinate system.

Therefore, bounds has the following features:

It refers to its own coordinate system. It can modify the origin position of its coordinate system, and thenPosition of the sub-view.

Iv. demo demonstration

`UIView * view1 = [[UIView alloc] initWithFrame: CGRectMake (20, 20,200,200)]; [view1 setBounds: CGRectMake (-30,-30,200,200)]; view1.backgroundColor = [UIColor redColor]; [self. view addSubview: view1]; // Add to self. view NSLog (@ "view1 frame: % ======== view1 bounds: % @", NSStringFromCGRect (view1.frame), NSStringFromCGRect (view1.bounds )); UIView * view2 = [[UIView alloc] initWithFrame: CGRectMake (0, 0,100,100)]; view2.backgroundColor = [UIColor yellowColor]; [view1 addSubview: view2]; // Add to view1, [In this case, the starting point in the upper left corner of the view1 coordinate system is (-30,-30)] NSLog (@ "view2 frame: % ======= view2 bounds: % @", NSStringFromCGRect (view2.frame), NSStringFromCGRect (view2.bounds ));`

This code is nothing special. Add view1 to view, and add view2 to view1. The second line of the Code sets the setBounds of view1. Comment out and open this line of code

[View1 setBounds: CGRectMake (-30,-30,200,200)];

This line of code changes the position of view2. Why does the offset (-30,-30) Allow view2 to move to the lower right corner?

This is because setBounds forces the point in the upper left corner of the self (view1) coordinate system to (-30,-30 ). The origin of view1 is naturally shifted to the lower right corner (30, 30 ).

The above code is output in the console as follows:

(Log output logs show that the default bounds of each new view are (), and the width and height of bounds are consistent with those of frame)

It's not over yet

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

Change the bounds of view1 to a larger value in the code segment above! Example: [view1 setBounds: CGRectMake (-30,-30,250,250)];

Log shows that the frame of view1 has been modified. This is because of the setBounds issue. :

Frame defines a frame (container) relative to the parent view, and bounds is the real display area. If bounds is smaller than frame, it can be placed in the frame (container. If the bounds is larger than the frame, it feels that the frame is "supported. The frame is changed to {25, 25}, {250,250. 25. How is it obtained? Bounds is longer than frame, and the width is greater than 50 pixels. Therefore, the four sides are balanced and each overflow is "25" pixels. :

V. Conclusion

Bounds has the following two features: bounds is a Rectangle, and the first half is the second half of the point is the size. These two attributes also indicate two features:

Bounds is like floating on a frame. A frame is a frame, and a bounds is a display of sub-views.

1. For bound's point: it does not change the origin of the frame. It changes the origin of bounds and affectsPosition of the sub-view. This function is more like moving the bounds origin.

2. For bound size: it can be changedFrame. If the bounds size is greater than the frame size. Then the frame will become larger, and the origin of the frame will also change. This function is more like a boundary.

It can be inferred that the first feature of setBound can be used for view sliding and gesture actions, because it can affect the display position of the subview.

For example, if I perform the setBound Function Multiple times within a period of time:

`    [view1 setBounds:CGRectMake(-0, 0, 200, 200)];    [view1 setBounds:CGRectMake(-10, 0, 200, 200)];    [view1 setBounds:CGRectMake(-20, 0, 200, 200)];    [view1 setBounds:CGRectMake(-30, 0, 200, 200)];    [view1 setBounds:CGRectMake(-40, 0, 200, 200)];    [view1 setBounds:CGRectMake(-50, 0, 200, 200)];    [view1 setBounds:CGRectMake(-60, 0, 200, 200)];    [view1 setBounds:CGRectMake(-70, 0, 200, 200)];    [view1 setBounds:CGRectMake(-80, 0, 200, 200)];    [view1 setBounds:CGRectMake(-90, 0, 200, 200)];    [view1 setBounds:CGRectMake(-100, 0, 200, 200)];`
The effect is as follows:

How to use the second feature? Find a case from the Internet: You can stretch the cell:

The Code is as follows. Just rewrite the layoutSubviews method of cell:

`// 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

What is the difference between setBounds and setSize in Java?

SetBounds
This is the location and size of the control button --> JButton

SetSize (); also controls JButton and JTextArea
JLabel

You can use it to know,

Java graphical interface (setBounds Tips)

SetBounds is valid only when LayoutManager of the parent container is empty.

Related Keywords:
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.