1.对autoresizing的简单介绍
autoresizing是对组件进行定位的一种技术。它不能和autolayout进行同时使用。并且在使用autoresizing的时候,只能控制父子视图之间的关系。
2.具体用法
第一种是在IB中,在Size inspector中进行设置;第二种方式是用代码的方法。下面是利用代码方法的例子:
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UIView *blueView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
self.blueView = blueView;
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 250, 100)];
redView.backgroundColor = [UIColor redColor];
redView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[blueView addSubview:redView];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGFloat w = 200 + arc4random_uniform(100);
CGFloat h = 200 + arc4random_uniform(100);
self.blueView.frame = CGRectMake(0, 0, w, h);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end