一、代码演示
1、设置第一个label
2、设置第二个label
3、设置第三个label
4、运行后的效果
例子2:
二、属性
/// 左侧
@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 *leading;
/// 底部
@property (nonatomic, strong, readonly) MASConstraint *trailing;
/// 宽
@property (nonatomic, strong, readonly) MASConstraint *width;
/// 高
@property (nonatomic, strong, readonly) MASConstraint *height;
/// 横向中点
@property (nonatomic, strong, readonly) MASConstraint *centerX;
/// 纵向中点
@property (nonatomic, strong, readonly) MASConstraint *centerY;
/// 文本基线
@property (nonatomic, strong, readonly) MASConstraint *baseline;
// 在Masonry的源码中我们可以看到他们对应的NSLayoutAttribute的属性对应如下
1、- (MASConstraint *)left {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeft];
}
2、- (MASConstraint *)top {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTop];
}
3、- (MASConstraint *)right {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRight];
}
4、- (MASConstraint *)bottom {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottom];
}
5、- (MASConstraint *)leading {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeading];
}
6、- (MASConstraint *)trailing {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailing];
}
7、- (MASConstraint *)width {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeWidth];
}
8、- (MASConstraint *)height {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeHeight];
}
9、- (MASConstraint *)centerX {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterX];
}
10、- (MASConstraint *)centerY {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterY];
}
11、- (MASConstraint *)baseline {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBaseline];
}
三、iOS8之后Masonry新出了几个属性:
/// 距离边框的距离,等同于选中Storyboard的Constrain to margins后加约束
@property (nonatomic, strong, readonly) MASConstraint *leftMargin;
@property (nonatomic, strong, readonly) MASConstraint *rightMargin;
@property (nonatomic, strong, readonly) MASConstraint *topMargin;
@property (nonatomic, strong, readonly) MASConstraint *bottomMargin;
@property (nonatomic, strong, readonly) MASConstraint *leadingMargin;
@property (nonatomic, strong, readonly) MASConstraint *trailingMargin;
@property (nonatomic, strong, readonly) MASConstraint *centerXWithinMargins;
@property (nonatomic, strong, readonly) MASConstraint *centerYWithinMargins;
- (MASConstraint *)leftMargin {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeftMargin];
}
- (MASConstraint *)rightMargin {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRightMargin];
}
- (MASConstraint *)topMargin {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTopMargin];
}
- (MASConstraint *)bottomMargin {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottomMargin];
}
- (MASConstraint *)leadingMargin {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeadingMargin];
}
- (MASConstraint *)trailingMargin {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailingMargin];
}
- (MASConstraint *)centerXWithinMargins {
return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterXWithinMargins];
}
网友评论