Reputation: 612
If I have a superview.frame = CGRectMake(0,0,200,200);
and a subview.frame = CGRectMake(0,0,100,100)
, subview has flexiable width and height, I think when superview's frame changes to (0,0,150,150)
, then subview's frame should change to (0, 0, 75, 75)
. But it's not.
- (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, 200)];
view1.backgroundColor = [UIColor blackColor];
[self.view addSubview:view1];
UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view2.backgroundColor = [UIColor redColor];
view2.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
[view1 addSubview:view2];
view1.frame = CGRectMake(0, 0, 150, 150);
[self printRect:view2.frame];
}
- (void) printRect:(CGRect)rect
{
NSLog(@"%0.1f, %0.1f, %0.1f, %0.1f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
}
Output:
0.0, 0.0, 50.0, 50.0
And when superview's frame changes to (0,0,100,100), subview's frame will become (0,0,0,0)
Why..
Upvotes: 1
Views: 491
Reputation: 588
in your code , view2
has fixed margins, which from top clockwise to left is (0, 100, 100, 0), this explain your view2
's autoresizing behaivor. your view2
's height and width wouldn't change only if you enlarge view1
. so you should add UIViewAutoresizingFlexibleBottomMargin
and UIViewAutoresizingFlexibleTopMargin
to your view2
's autoresziing mask
Upvotes: 0
Reputation: 38259
Replace your line of code with mine as u forgot UIViewAutoresizingFlexibleBottomMargin
and UIViewAutoresizingFlexibleTopMargin
view2.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
Upvotes: 2