The difference between frame and bounds in iOS development-view

Source: Internet
Author: User

The development of the time to adjust the view will often encounter frame and bounds, just start to look at the time is not very clear, but looked at the official document, frame is to determine the view in the parent view of the position, and the size of itself, bounds determines the child view in the current view of the position, You can also change the size of the view, and if bounds determines the size, then the view takes precedence over the width of the bounds in the selection. The center position is relative to the location in the parent coordinate system. Apple official gave a picture for reference:

If it's not clear yet, refer to the code in frame and bounds:

-(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);}

Of course, in order to better understand the above concept, write a small demo, viewdidload add the picture in the view, and then add a view to the picture:

Uiimageview  *girlimageview=[[uiimageview alloc]initwithimage:[uiimage imagenamed:@ "Girl.jpg"];    Girlimageview.frame=cgrectmake (max., +--);    [Self.view addsubview:girlimageview];//added to the parent view    NSLog (@ "Girlimageview frame:%@------girlimageview bounds:%@", Nsstringfromcgrect (Girlimageview.frame), Nsstringfromcgrect (Girlimageview.bounds));    UIView *childview = [[UIView alloc] Initwithframe:cgrectmake (0, 0, +)];    Childview.backgroundcolor = [Uicolor yellowcolor];    [Girlimageview Addsubview:childview];    NSLog (@ "Childview frame:%@-------childview bounds:%@", Nsstringfromcgrect (Childview.frame), Nsstringfromcgrect ( Childview.bounds));

The specific results are as follows:

Output Result:

2015-04-03 11:50:00.326 mycontraint[1929:98282] Girlimageview frame:{{40, +----------Girlimageview bounds:{{ 0, 0}, {300}}2015-04-03, 11:50:00.327 mycontraint[1929:98282] Childview frame:{{0, 0}, {, +}}-------Childview bo unds:{{0, 0}, {100, 100}}

This time if you change the Uiimageview bounds, the code is as follows:

   Uiimageview  *girlimageview=[[uiimageview alloc]initwithimage:[uiimage imagenamed:@ "Girl.jpg"];    Girlimageview.frame=cgrectmake (max., +--);    Changing the position of the internal origin in the view can also change the frame's width and height    [girlimageview setbounds:cgrectmake ( -30, -30, +)];    [Self.view addsubview:girlimageview];//added to the parent view    NSLog (@ "Girlimageview frame:%@------girlimageview bounds:%@", Nsstringfromcgrect (Girlimageview.frame), Nsstringfromcgrect (girlimageview.bounds));

Picture effect:

The printing effect is as follows:

2015-04-03 11:57:06.738 mycontraint[2019:102665] Girlimageview frame:{{40,--------------girlimageview bounds: {{ -30, -30}, {230}}2015-04-03, 11:57:06.739 mycontraint[2019:102665] Childview frame:{{0, 0}, {$, +}}------- Childview bounds:{{0, 0}, {100, 100}}

Through the above code demonstration, the relationship between frame and bounds should be very clear, if there is doubt, Huan drink together to explore ~

The difference between frame and bounds in iOS development-view

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.