How to autoresize subview
This seems simple and works on views created in Interface Builder but I can't get it work on views created programmatically. I'm trying to resize subviews proportionally when the superview is resized but it's not working. Here's the simplified version of the code:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
v.backgroundColor = [UIColor redColor];
[self.view addSubview:v];
v.autoresizesSubviews = YES;
v.autoresizingMask = UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleWidth;
UIView *v2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
v2.backgroundColor = [UIColor blueColor];
[v addSubview:v2];
v.frame = CGRectMake(20, 20, 50, 50);
[v release];
[v2 release];
}
Please help. Thanks.
|