iOS view frame vs. bounds

Source: Internet
Author: User

Bounds coordinates: Your own defined coordinate system, Setbound indicates the coordinates of the upper-left corner of this view in the coordinate system,

Default value (0,0)

Frame coordinates: The coordinates of the upper-left corner of the child view in the parent view coordinate system (bounds coordinate system), default value (0,0)

Child view Actual position = Parent View actual location-parent view bounds coordinates + child view frame coordinates

First, bounds

Affects only the position of the child view relative to the screen, and does not affect its relative screen position when modified

  1. When the parent view bounds coordinates (0,0)

- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.NSLog (@"View Frame:%@========view bounds:%@", Nsstringfromcgrect (Self.view.frame), Nsstringfromcgrect (self.view.bounds)); UIView*view1 = [[UIView alloc] Initwithframe:cgrectmake ( -, -, $,260)]; View1.backgroundcolor=[Uicolor Redcolor];        [Self.view Addsubview:view1]; NSLog (@"View Frame:%@========view bounds:%@", Nsstringfromcgrect (Self.view.frame), Nsstringfromcgrect (Self.view.bounds));}

  

  2. When the parent view bounds coordinates ( -20,-20)

- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.NSLog (@"View Frame:%@========view bounds:%@", Nsstringfromcgrect (Self.view.frame), Nsstringfromcgrect (self.view.bounds)); UIView*view1 = [[UIView alloc] Initwithframe:cgrectmake ( -, -, $,260)]; View1.backgroundcolor=[Uicolor Redcolor];    [Self.view Addsubview:view1]; [Self.view Setbounds:cgrectmake (- -, - -, the,568)]; NSLog (@"View Frame:%@========view bounds:%@", Nsstringfromcgrect (Self.view.frame), Nsstringfromcgrect (Self.view.bounds));}

  

Second, frame

The modification changed its position in the parent view coordinate system (bounds coordinate system), its own position and

The child view location will be changed.

1. Parent view frame coordinates (0,0)

- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.UIView*view1 = [[UIView alloc] Initwithframe:cgrectmake (0,0, $,260)]; View1.backgroundcolor=[Uicolor Redcolor];        [Self.view Addsubview:view1]; UIView*view2 = [[UIView alloc] Initwithframe:cgrectmake (0,0, -, -)]; View2.backgroundcolor=[Uicolor Yellowcolor];        [View1 ADDSUBVIEW:VIEW2]; NSLog (@"View Frame:%@========view bounds:%@", Nsstringfromcgrect (View1.frame), Nsstringfromcgrect (view1.bounds)); NSLog (@"View Frame:%@========view bounds:%@", Nsstringfromcgrect (View2.frame), Nsstringfromcgrect (View2.bounds));}

  

2. Parent view frame coordinates (60,60)

- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.UIView*view1 = [[UIView alloc] Initwithframe:cgrectmake ( -, -, $,260)]; View1.backgroundcolor=[Uicolor Redcolor];        [Self.view Addsubview:view1]; UIView*view2 = [[UIView alloc] Initwithframe:cgrectmake (0,0, -, -)]; View2.backgroundcolor=[Uicolor Yellowcolor];        [View1 ADDSUBVIEW:VIEW2]; NSLog (@"View Frame:%@========view bounds:%@", Nsstringfromcgrect (View1.frame), Nsstringfromcgrect (view1.bounds)); NSLog (@"View Frame:%@========view bounds:%@", Nsstringfromcgrect (View2.frame), Nsstringfromcgrect (View2.bounds));}

  

Third, the description

  The root view can only modify the bounds coordinates, not the frame coordinates

The following self.view are the root view

  1. Initial state

-(void) viewdidload {    [super Viewdidload];     // additional setup after loading the view, typically from a nib.  *view1 = [[UIView alloc] Initwithframe:cgrectmake (260          )];     = [Uicolor redcolor];    [Self.view addsubview:view1];}

  

  2. Modify bounds Coordinates: valid

- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.UIView*view1 = [[UIView alloc] Initwithframe:cgrectmake ( -, -, $,260)]; View1.backgroundcolor=[Uicolor Redcolor];    [Self.view Addsubview:view1]; CGRect Viewbounds=Self.view.bounds; VIEWBOUNDS.ORIGIN.Y=- +; Viewbounds.origin.x=- +; Self.view.bounds=Viewbounds; NSLog (@"View Frame:%@========view bounds:%@", Nsstringfromcgrect (Self.view.frame), Nsstringfromcgrect (Self.view.bounds));}

   

   

  3. Change frame Coordinates: invalid

- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.UIView*view1 = [[UIView alloc] Initwithframe:cgrectmake ( -, -, $,260)]; View1.backgroundcolor=[Uicolor Redcolor];    [Self.view Addsubview:view1]; CGRect Viewframe=Self.view.frame; VIEWFRAME.ORIGIN.Y= -; Viewframe.origin.x= -; Self.view.frame=Viewframe; NSLog (@"View Frame:%@========view bounds:%@", Nsstringfromcgrect (Self.view.frame), Nsstringfromcgrect (Self.view.bounds));}

  

  

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.