The difference between frame and bounds of IOS view (location and size)

Source: Internet
Author: User

First, make a list of accepted information:

See the following code first you must understand something:

123456 -(CGRect)frame{    returnCGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width,self.frame.size.height);}-(CGRect)bounds{    returnCGRectMake(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, unless considered setbounds), and the frame's origin is arbitrary (relative to the coordinate position in the parent view).

Look at sketch and you'll see.

    • 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 electricity is, father's coordinate system)

Personally, I think, bounds a little puzzled, a little 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, of course, the top layer of 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, I personally think that bounds affects the location and size of the child view.

Second, demo Demo:

12345678910 UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, 250)];  [view1 setBounds:CGRectMake(-20, -20, 280, 250)];  view1.backgroundColor = [UIColor redColor];  [self.view addSubview:view1];//添加到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];//添加到view1上,[此时view1坐标系左上角起点为(-20,-20)]  NSLog(@"view2 frame:%@========view2 bounds:%@",NSStringFromCGRect(view2.frame),NSStringFromCGRect(view2.bounds));

(Run the show, the picture said very clearly OH)

(log output logs indicate that each new view default bounds is actually (0,0))

Transferred from: http://www.cocoachina.com/ios/20150623/12187.html

The difference between frame and bounds of IOS view (location and size)

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.