Masonry 基本用法
添加:mas_makeConstraints()-添加約束
更新:mas_updateConstraints()-更新約束
刪除:mas_remakeConstraints()-有些時候修改約束沒有用就用這個
----------
距左:make.left.equalTo(self.view).with.offset(10);
距右:make.right.equalTo(self.view).with.offset(-10);
距上:make.top.equalTo(self.view).with.offset(10);
距下:make.bottom.equalTo(self.view).with.offset(-10);
居中:make.center.equalTo(self.view)
總合:make.edges.equalTo(superview).insets(UIEdgeInsetsMake(5, 10, 15, 20))
垂直居中:make.centerY.equalTo(self.view)
水平居中:make.centerX.equalTo(self.view)
width() 和 mas_width()區(qū)別焰轻?
width():用來表示寬度乞旦,例如代表view的寬度
mas_width():用來獲取寬度的值愈涩。和上面的區(qū)別在于捧灰,一個代表某個坐標(biāo)系對象喳钟,一個用來獲取坐標(biāo)系對象的值
mas_equalTo和equalTo區(qū)別律想?
mas_equalTo 比equalTo多了類型轉(zhuǎn)換操作,一般來說殴蹄,大多數(shù)時候兩個方法都是通用的究抓,但是對于數(shù)值元素使用mas_equalTo猾担。對于對象或是多個屬性的處理袭灯,使用equalTo刺下。特別是多個屬性時,必須使用equalTo,例如 make.left.and.right.equalTo(self.view);
修正
- offset(位移)修正
- multipliedBy(倍率)修正
比例
-*寬是父視圖的 1:3
make.width.mas_equalTo(superView).multipliedBy(1.00/3);
-*高是父視圖的 1:4
make.height.mas_equalTo(superView).multipliedBy(0.25);
寬高合并寫法
-*寬
make.width.mas_equalTo(60);
-*高
make.height.mas_equalTo(60);
-*總合
make.size.mas_equalTo(CGSizeMake(50, 100));
取值范圍
-*大于等于
greaterThanOrEqualTo();
-*小于等于
lessThanOrEqualTo();
-*為寬(高)設(shè)置范圍例如:200 ≤ width ≤ 400
make.width.greaterThanOrEqualTo(@200);
make.width.lessThanOrEqualTo(@400);
優(yōu)先級
priority -允許你設(shè)置一個非常準(zhǔn)確的的約束優(yōu)先級(0-1000)值越大優(yōu)先級越高
make.left.mas_equalTo(20).priority(700);
priorityHigh -高
priorityMedium -中
priorityLow -低
不想添加mas_前綴稽荧,還想使用常量值設(shè)置約束
需要在導(dǎo)入Masonry頭文件前橘茉,設(shè)置宏定義
MAS_SHORTHAND_GLOBALS
,至于為什么姨丈,去Masonry代碼中搜索一下MAS_SHORTHAND_GLOBALS
便知畅卓。
等寬、等間距布局view
等寬蟋恬、等間距布局view
//.等寬翁潘、等間距布局view
UIView *view1 = [[UIView alloc] init];
view1.backgroundColor = [UIColor blueColor];
[self.view addSubview:view1];
UIView *view2 = [[UIView alloc] init];
view2.backgroundColor = [UIColor redColor];
[self.view addSubview:view2];
UIView *view3 = [[UIView alloc] init];
view3.backgroundColor = [UIColor greenColor];
[self.view addSubview:view3];
self.array = @[view1,view2,view3];
//橫向
- (void)getHorizontalone
{
//方法一,array 的 mas_distributeViewsAlongAxis
/**
* 多個控件固定間隔的等間隔排列,變化的是控件的長度或者寬度值
*
* @param axisType 軸線方向
* @param fixedSpacing 間隔大小
* @param leadSpacing 頭部間隔
* @param tailSpacing 尾部間隔
*/
// MASAxisTypeHorizontal 水平
// MASAxisTypeVertical 垂直
[self.array mas_distributeViewsAlongAxis:MASAxisTypeHorizontal
withFixedSpacing:5
leadSpacing:5
tailSpacing:5];
[self.array mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(60);//設(shè)置距離頂部的約束
make.height.mas_offset(100);//設(shè)置高
}];
}
設(shè)置寬度間距動態(tài)
設(shè)置寬度間距動態(tài)效果圖
//在紅色View里面放三個正方形藍(lán)色View, 寬度均為30, 間隙一樣大
NSMutableArray *blueViews = [NSMutableArray array];
for (NSInteger i = 0; i < 3; i++) {
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[redView addSubview:blueView];
[blueViews addObject:blueView];
}
CGFloat padding2 = (300 - 3 * 30) / 4;
[blueViews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:30 leadSpacing:padding2 tailSpacing:padding2];
[blueViews mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(redView);
UIView *blueView = (UIView *)blueViews[0];
make.height.mas_equalTo(blueView.mas_width);
}];
等間距但寬度不同
等間距但寬度不同
//在紅色View里面放三個大小不一樣的綠色正方形, 間隙等大, masonry并沒提供相關(guān)方法
NSMutableArray *greenViews = [NSMutableArray array];
for (NSInteger i = 0; i < 3; i++) {
UIView *greenView = [[UIView alloc] init];
greenView.backgroundColor = [UIColor greenColor];
[redView addSubview:greenView];
[greenViews addObject:greenView];
[greenView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(redView).offset(-10);
make.width.mas_equalTo(i*20 + 20);
make.height.mas_equalTo(greenView.mas_width);
}];
}
[redView distributeSpacingHorizontallyWith:greenViews];
Masonry 注意事項
- 使用
mas_makeConstraints
方法的元素必須事先添加到父元素
中歼争,例如[self.view addSubview:view]
;- 注意
with
和width
不要寫混拜马,報錯不易查找with
和and
方法其實沒有做任何操作,方法只是返回對象本身沐绒,這兩個方法的左右完全是為了方法寫的時候的可讀性 俩莽。