主要屬性
@property (nonatomic, strong, readonly) MASConstraint *left;//左側(cè)
@property (nonatomic, strong, readonly) MASConstraint *top;//上側(cè)
@property (nonatomic, strong, readonly) MASConstraint *right;//右側(cè)
@property (nonatomic, strong, readonly) MASConstraint *bottom;//下側(cè)
@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;//文本基線
術(shù)語解釋
- 必須先將視圖添加至父視圖(
[fatherView addSubview:View]
),才能進(jìn)行設(shè)置。 - 在Masonry中,and,with都沒有具體操作,僅僅是為了提高程序的可讀性。
make.left.and.top.mas_equalTo(20);
等價于
make.left.top.mas_equalTo(20);
- 設(shè)置距離數(shù)值時,正數(shù)為向右的距離,負(fù)數(shù)為向左相距的距離。
- 如果約束條件是數(shù)值或者結(jié)構(gòu)體等類型,可以使用mas_equalTo進(jìn)行包裝。
一般將數(shù)值類型的約束用mas_equalTo,而相對于某個控件,或者某個控件的某個約束,使用equalTo,如:
make.size.mas_equalTo(CGSizeMake(100, 100));
make.center.equalTo(weakSelf.view);
約束函數(shù)
添加大小約束(大小為100 *100)
make.size.mas_equalTo(CGSizeMake(100, 100));
居中約束
make.center.equalTo(weakSelf.view);
與左邊界約束(本視圖左側(cè)與父視圖邊界相切為0)
make.left.equalTo(self.view).offset(0);
與其他控件寬度約束(本視圖的寬度與其余兩個視圖寬度相等)
make.width.equalTo(@[topView,bottomView]);
設(shè)置兩個控件之間的距離(頂部與上一個視圖底部相距150)
make.top.mas_equalTo(topView.mas_bottom).offset(150);
與左、下均相切
make.left.bottom.equalTo(self.view).offset(0);
設(shè)置寬和高的約束(均為150)
make.width.height.mas_equalTo(150);
設(shè)置兩個控件關(guān)聯(lián)(該控件與目標(biāo)控件高度一致【縱坐標(biāo)相等】)
make.centerY.mas_equalTo(imageView1);