1 //设置约束 2 - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block; 3 4 //如果之前已经有约束,则更新新的约束,如果没有约束,则添加约束 5 - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block; 6 7 //将之前的约束全部删除,添加新的约束 8 - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;
以下为代码使用实现布局效果:
1 //添加两个控件 2 UIView *blueView = [[UIView alloc] init]; 3 blueView.backgroundColor = [UIColor blueColor]; 4 blueView.translatesAutoresizingMaskIntoConstraints = NO; 5 [self.view addSubview:blueView]; 6 7 UIView *redView = [[UIView alloc] init]; 8 redView.backgroundColor = [UIColor redColor]; 9 redView.translatesAutoresizingMaskIntoConstraints = NO; 10 [self.view addSubview:redView]; 11 12 //给蓝色View设置约束 13 [blueView mas_makeConstraints:^(MASConstraintMaker *make) { 14 make.left.equalTo(self.view.mas_left).offset(30);//和父view的左边间距为30; 15 make.bottom.equalTo(self.view.mas_bottom).offset(-30);//和父view的底部间距为30; 16 make.right.equalTo(redView.mas_left).offset(-30);//和红色view的间距为30; 17 make.height.mas_equalTo(50);//蓝色view的高度为50 18 }]; 19 20 //给红色View设置约束 21 [redView mas_makeConstraints:^(MASConstraintMaker *make) { 22 make.right.equalTo(self.view.mas_right).offset(-30);//和父view的右边间距为30; 23 make.bottom.equalTo(blueView.mas_bottom);//和蓝色view的底部对齐 24 make.height.equalTo(blueView.mas_height);//和蓝色view的高度相等 25 make.width.equalTo(blueView.mas_width);//和蓝色view的宽度相等 26 }];
效果图如下: