Comparison between frame and bounds in ios View

Source: Internet
Author: User

Bounds coordinates: Custom coordinate system. setbound specifies the coordinates in the upper-left corner of this view in this coordinate system,

Default Value (0, 0)

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

Actual position of the Child view = actual position of the parent view-bounds coordinates of the parent view + frame coordinates of the Child View

I. bounds

Only the position of the sub-view relative to the screen is affected. The position of the sub-view relative to the screen is not affected when the sub-view is modified.

1. When the bounds coordinate of the parent view is (0, 0)

- (void)viewDidLoad {    [super viewDidLoad];    // Do any 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(20, 20, 200, 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 bounds coordinate of the parent view is (-20,-20)

- (void)viewDidLoad {    [super viewDidLoad];    // Do any 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(20, 20, 200, 260)];    view1.backgroundColor = [UIColor redColor];    [self.view addSubview:view1];    [self.view setBounds:CGRectMake(-20, -20, 320, 568)];        NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(self.view.frame),NSStringFromCGRect(self.view.bounds));}

  

Ii. frame

The position, position, and

The position of the sub-view is changed.

1. coordinate of the parent View frame (0, 0)

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 260)];    view1.backgroundColor = [UIColor redColor];    [self.view addSubview:view1];        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];    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. coordinates of the parent View frame (60, 60)

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(60, 60, 200, 260)];    view1.backgroundColor = [UIColor redColor];    [self.view addSubview:view1];        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];    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));}

  

Iii. Description

The Root View can only modify bounds coordinates, but cannot modify the frame coordinates.

The following self. view is the root view.

1. Initial status

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 260)];    view1.backgroundColor = [UIColor redColor];    [self.view addSubview:view1];}

  

2. Modify bounds coordinates: VALID

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 260)];    view1.backgroundColor = [UIColor redColor];    [self.view addSubview:view1];    CGRect viewBounds = self.view.bounds;    viewBounds.origin.y=-40;    viewBounds.origin.x=-40;    self.view.bounds=viewBounds;        NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(self.view.frame),NSStringFromCGRect(self.view.bounds));}

   

   

3. Modify the frame coordinate: Invalid

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 260)];    view1.backgroundColor = [UIColor redColor];    [self.view addSubview:view1];    CGRect viewFrame = self.view.frame;    viewFrame.origin.y=60;    viewFrame.origin.x=60;    self.view.frame=viewFrame;        NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(self.view.frame),NSStringFromCGRect(self.view.bounds));}

  

  

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.