#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
//约束
//Block作为参数 ---- 函数式编程思想
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
//make ---- 链式编程
make.left.top.equalTo(@10);
make.right.bottom.equalTo(@-10);
}];
}
@end
mas_makeConstraints执行的流程:
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
self.translatesAutoresizingMaskIntoConstraints = NO;
MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
block(constraintMaker);
return [constraintMaker install];
}
self.translatesAutoresizingMaskIntoConstraints = NO;
***关掉原来旧版本的自动布局(Autoresizing)
MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
1.创建约束制造者MASConstraintMaker,并且绑定控件
block(constraintMaker);
2.执行mas_makeConstraints传入的Block
[constraintMaker install];
3.让约束制造者安装约束!
- (NSArray *)install {
if (self.removeExisting) {
NSArray *installedConstraints = [MASViewConstraint installedConstraintsForView:self.view];
for (MASConstraint *constraint in installedConstraints) {
[constraint uninstall];
}
}
NSArray *constraints = self.constraints.copy;
for (MASConstraint *constraint in constraints) {
constraint.updateExisting = self.updateExisting;
[constraint install];
}
[self.constraints removeAllObjects];
return constraints;
}
[constraint uninstall];
*1.清空之前所有的约束
[constraint install];
*2.遍历约束数组,安装约束