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