Masonry(第三方库)的使—代码实现屏幕适配
#import"Masonry"
实现效果图
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. [self testBasicLayout]; [self testThreeLayout]; } - (void)testThreeLayout { UIView *redView = [[UIView alloc] init]; redView.backgroundColor = [UIColor redColor]; [self.view addSubview:redView]; UIView *blueView = [[UIView alloc] init]; blueView.backgroundColor = [UIColor blueColor]; [self.view addSubview:blueView]; UIView *greenView = [[UIView alloc] init]; greenView.backgroundColor = [UIColor greenColor]; [self.view addSubview:greenView]; [redView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(50); make.left.mas_equalTo(50); make.right.mas_equalTo(-50); make.height.mas_equalTo(100); }]; [blueView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(redView.mas_bottom).offset(50);//偏移量 默认0 make.left.mas_equalTo(50); make.height.mas_equalTo(100); //make.width.mas_equalTo(100); }]; [greenView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(redView.mas_bottom).offset(50); make.right.mas_equalTo(-50); make.height.mas_equalTo(100); make.width.mas_equalTo(100); //间隔固定,宽度相等 make.left.mas_equalTo(blueView.mas_right).offset(50); make.width.mas_equalTo(blueView.mas_width); }]; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
- (void)testBasicLayout { //靠右对齐,100x100,top-50 right-50 UIView *redView = [[UIView alloc] init]; redView.backgroundColor = [UIColor redColor]; [self.view addSubview:redView]; //block重要参数:mark参数,设置make的属性。控制布局 /* [redView mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(100); make.height.mas_equalTo(100); make.top.mas_equalTo(50); make.right.mas_equalTo(-50); }]; */ [redView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(50); make.left.mas_equalTo(50); make.right.mas_equalTo(-50); make.height.mas_equalTo(100); }]; }