Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a views bounds affect the frame

I'm trying to understand how a view responds to its bounds being changed. If I change the bounds origin of a view, will it change the frame origin accordingly?

E.g.

UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(150, 150, 150, 200)];
greenView.backgroundColor = [UIColor colorWithRed:0.494 green:0.827
                                             blue:0.129 alpha:1];
[self.view addSubview:greenView];
greenView.bounds = CGRectMake(0, 150, greenView.bounds.size.width, greenView.bounds.size.height);

Would this not change the frame's origin to (150, 300)? Running the code above doesn't seem to change its frame. (I know you're not meant to change a views position using bounds, this is just a hypothetical).

like image 246
Smashkon Avatar asked Jan 07 '23 08:01

Smashkon


1 Answers

Per Apple Documentation, here are the relationship between a view's frame, bounds and centre:

Although you can change the frame, bounds, and center properties independent of the others, changes to one property affect the others in the following ways:

  • When you set the frame property, the size value in the bounds property changes to match the new size of the frame rectangle. The
    value in the center property similarly changes to match the new
    center point of the frame rectangle.
  • When you set the center property, the origin value in the frame changes accordingly.
  • When you set the size of the bounds property, the size value in the frame property changes to match the new size of the bounds rectangle.

So, answer to your question, changing X,Y position on View's bounds should not affect frame. Most of the cases bounds starts with (0,0). Changing the height or width to negative values would allow origin of bounds to go negative.

EDIT: To answer OP question - No, changing the position of bounds won't affect frame in any way. Since bounds is with reference to view's own co-ordinate system, changing X,Y on self co-ordinate system would not change position in superview's co-ordinate system.

You can try by using two custom views like this:

UIView* view1 = [[UIView alloc] initWithFrame:CGRectMake(50.0f, 100.0f, 150.0f, 150.0f)];
view1.backgroundColor = [UIColor redColor];

NSLog(@"view1.bounds = %@", NSStringFromCGRect(view1.bounds));
NSLog(@"view1.frame = %@", NSStringFromCGRect(view1.frame));

UIView* view2 = [[UIView alloc] initWithFrame:CGRectInset(view1.bounds, 20.0f, 20.0f)];
view2.backgroundColor = [UIColor yellowColor];

NSLog(@"view2.bounds = %@", NSStringFromCGRect(view2.bounds));
NSLog(@"view2.frame = %@", NSStringFromCGRect(view2.frame));

NSLog(@"view1.bounds = %@", NSStringFromCGRect(view1.bounds));
NSLog(@"view1.frame = %@", NSStringFromCGRect(view1.frame));

NSLog(@"view2.bounds = %@", NSStringFromCGRect(view2.bounds));
NSLog(@"view2.frame = %@", NSStringFromCGRect(view2.frame));

[view1 addSubview:view2];

And then change the subview bound like this:

CGRect frame = view2.bounds;
frame.origin.x += 20.0f;
frame.origin.y += 20.0f;
view2.bounds = frame;

Changing the bounds would not impact frame at all. And both the views would look same on screen:

enter image description here enter image description here

And finally, try by changing the bounds of parent view to see below screen:

CGRect frame = view1.bounds;
frame.origin.x += 20.0f;
frame.origin.y += 20.0f;
view1.bounds = frame;

enter image description here

like image 176
Abhinav Avatar answered Jan 17 '23 13:01

Abhinav