UIView *view1 = [[UIViewalloc] init];
UIView *view2 = [[UIViewalloc] init];
UIView *view3 = [[UIViewalloc] init];
view1.backgroundColor = [UIColorblackColor];
view2.backgroundColor = [UIColorredColor];
view3.backgroundColor = [UIColorgrayColor];
//1 .创建一个view,使其在屏幕居中
[self.viewaddSubview:view1];
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.size.mas_equalTo(CGSizeMake(300,300));
}];
//在Masonry中能够添加autolayout约束有三个函数
//- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
//- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
//- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;
/*
mas_makeConstraints 只负责新增约束 Autolayout不能同时存在两条针对于同一对象的约束 否则会报错
mas_updateConstraints 针对上面的情况 会更新在block中出现的约束 不会导致出现两个相同约束的情况
mas_remakeConstraints 则会清除之前的所有约束 仅保留最新的约束
*/
//2. 创建一个view2,让其略小于去superview(边距为10)
[view1 addSubview:view2];
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(view1).with.insets(UIEdgeInsetsMake(10,10, 10, 10));
//等价于
//make.top.equalTo(view1).with.offset(10);
//make.left.equalTo(view1).with.offset(10);
//make.bottom.equalTo(view1).with.offset(-10);
//make.right.equalTo(view1).with.offset(-10);
//也等价于
//make.top.left.bottom.and.right.equalTo(view1).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
}];
}
http://www.cocoachina.com/ios/20141219/10702.html