随着iphone大屏时代的到来,ios开发中的界面适配变的不再容易,frame已经变的有点鸡肋。
@property (nonatomic, strong, readonly) MASConstraint *left;
@property (nonatomic, strong, readonly) MASConstraint *top;
@property (nonatomic, strong, readonly) MASConstraint *right;
@property (nonatomic, strong, readonly) MASConstraint *bottom;
@property (nonatomic, strong, readonly) MASConstraint *width;
@property (nonatomic, strong, readonly) MASConstraint *height;
@property (nonatomic, strong, readonly) MASConstraint *centerX;
@property (nonatomic, strong, readonly) MASConstraint *centerY;
UIView * view = [[UIView alloc] init];
view.frame = CGRectMake(50, 100, 80, 40);
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];
UIView * view = [[UIView alloc] init];
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];
Bself(bself);
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(bself.view).with.offset(50);
make.top.equalTo(bself.view).with.offset(100);
make.width.mas_equalTo(@80);
make.height.mas_equalTo(@40);
}];
下面解释一下
//block里不能直接使用self,要用下面这个宏转换一下(#define Bself(weakSelf) __weak __typeof(&*self)weakSelf = self;)
Bself(bself);
//Autolayout用调用的block
[view mas_makeConstraints:^(MASConstraintMaker *make) {
//距self.view的左边50
make.left.equalTo(bself.view).with.offset(50);
//距self.voew的顶上100
make.top.equalTo(bself.view).with.offset(100);
//宽固定为80
make.width.mas_equalTo(@80);
//高固定为40
make.height.mas_equalTo(@40);
}];
Masonry是用block进行回调,添加约束有三个方法:
- (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 则会清除之前的所有约束 仅保留最新的约束
*/
UIView * view2 = [[UIView alloc] init];
view2.backgroundColor = [UIColor blueColor];
[self.view addSubview:view2];
Bself(bself);
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(bself.view).with.offset(20);
make.bottom.equalTo(bself.view).with.offset(-20);
make.left.equalTo(bself.view).with.offset(20);
make.right.equalTo(bself.view).with.offset(-20);
/*
等价于
make.edges.equalTo(bself.view).with.insets(UIEdgeInsetsMake(20, 20, 20, 20));
*/
}];
记得右和下是负数,如果是正数的话就变成超出右边或下边20了。效果图如下UIView * view3 = [[UIView alloc] init];
view3.backgroundColor = [UIColor greenColor];
[view2 addSubview:view3];
UIView * view4 = [[UIView alloc] init];
view4.backgroundColor = [UIColor greenColor];
[view2 addSubview:view4];
[view3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(view2.mas_centerX);
make.top.equalTo(view2).with.offset(20);
make.bottom.equalTo(view4.mas_top).with.offset(-20);
make.width.mas_equalTo(@200);
make.height.equalTo(view4);
}];
[view4 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(view2.mas_centerX);
make.top.equalTo(view4.mas_bottom).with.offset(20);
make.bottom.equalTo(view2).with.offset(-20);
make.width.mas_equalTo(@200);
make.height.equalTo(view3);
}];
效果如下[view3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(view2.mas_centerX);
make.top.equalTo(view2).with.offset(20);
make.bottom.equalTo(view4.mas_top).with.offset(-20);
make.width.mas_equalTo(@200);
//高度是view4两倍
make.height.equalTo(view4).with.multipliedBy(2);
}];
[view4 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(view2.mas_centerX);
make.top.equalTo(view4.mas_bottom).with.offset(20);
make.bottom.equalTo(view2).with.offset(-20);
make.width.mas_equalTo(@200);
//高度是view3的高度除以2
make.height.equalTo(view3).with.dividedBy(2);
}];