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..
Replace your line of code with mine as u forgot UIViewAutoresizingFlexibleBottomMargin
and UIViewAutoresizingFlexibleTopMargin
view2.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;