Masonry是什么摊滔?
Masonry是一個輕量級的布局框架函似,擁有自身的描述性語法槐脏,采用更加優(yōu)雅的鏈式語法封裝了自動布局autolayout,簡潔明了撇寞,可讀性高顿天,并且同時支持iOS和Mac OSXMasonry
Masonry在GitHub
Masonry怎么用?
首先我們看一下官方的示例代碼:
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(superview).with.insets(padding);
}];
Masonry的一些屬性與NSLayoutAttribute對照
MASViewAttribute | NSAutoLayout | 說明 |
---|---|---|
view.mas_left | NSLayoutAttributeLeft | 左側 |
view.mas_right | NSLayoutAttributeRight | 右側 |
view.mas_top | NSLayoutAttributeTop | 上側 |
view.mas_bottom | NSLayoutAttributeBottom | 下側 |
view.mas_leading | NSLayoutAttributeLeading | 首部 |
view.mas_trailing | NSLayoutAttributeTrailing | 尾部 |
view.mas_width | NSLayoutAttributeWidth | 寬 |
view.mas_height | NSLayoutAttributeHeight | 高 |
view.mas_centerX | NSLayoutAttributeCenterX | 橫向中點 |
view.mas_centerY | NSLayoutAttributeCenterY | 縱向中點 |
view.mas_baseline | NSLayoutAttributeBaseline | 文本基線 |
舉例:
1. 居中顯示一個View:
UIView *view1 = [[UIView alloc] init];
view1.backgroundColor = [UIColor redColor];
//2.使用Masonry約束之前,一定要將view加到superView上蔑担,否則會報錯
[self.view addSubview:view1];
//用masonry函數(shù)對你添加的view進行約束
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
// 設置view1居中
make.center.equalTo(self.view);
// 設置view2寬高
make.size.mas_equalTo(CGSizeMake(300, 300));
}];
/**
*1.mas_makeConstraints 只負責添加新增約束牌废,Autolayout中不能同時存在兩條針對于同一對象的約束,否則會報錯
*2.mas_makeConstraints 針對上面的情況啤握,會更新在block中出現(xiàn)的約束鸟缕,確保不會出現(xiàn)兩個相同的約束
*3.mas_makeConstraints 會清除之前所有的約束,僅保留最新的約束
*/
/**
*mas_equal 除了支持NSNumber的數(shù)值類型外排抬,就支持CGPoint CGSize UIEdgeInsets
*/
2.讓一個view略小于其superView[邊界15];
UIView *view2 = [[UIView alloc] init];
view2.backgroundColor = [UIColor cyanColor];
[self.view addSubview:view2];
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
//1.第一種布局方法
make.top.equalTo(self.view).with.offset(15);
make.left.equalTo(self.view).with.offset(15);
make.bottom.equalTo(self.view).with.offset(-15);
make.right.equalTo(self.view).with.offset(-15);
//2.第二種
// make.top.left.bottom.and.right.equalTo(self.view).insets(UIEdgeInsetsMake(15, 15, 15, 15));
//3.第三種
// make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(15, 15, 15, 15));
}];
3.讓兩個高度為150 的veiw垂直居中并且等寬等間隔排列 間隔(10) 寬度自動計算
UIView *view3 = [[UIView alloc] init];
view3.backgroundColor = [UIColor purpleColor];
[self.view addSubview:view3];
UIView *view4 = [[UIView alloc] init];
view4.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view4];
[view3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.view.mas_centerY);
make.left.equalTo(self.view.mas_left).with.offset(10);
make.right.equalTo(view4.mas_left).with.offset(-10);
make.height.mas_equalTo(@150);
make.width.equalTo(view4);
}];
[view4 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.view.mas_centerY);
make.left.equalTo(view3.mas_right).with.offset(10);
make.right.equalTo(self.view.mas_right).with.offset(-10);
make.height.equalTo(view3);
make.width.equalTo(view3);
}];
使用Masonry過程中可能出現(xiàn)的錯誤:
一.錯誤信息統(tǒng)計(width改為with)
reason: 'Attributes should be chained before defining the constraint relation'
崩潰到masonry內部的方法里面:
崩潰的提示信息:
直接上代碼:(這是運行沒有問題的代碼)
[self.GradientLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.CurrenPriceLabel.mas_right);
make.left.equalTo(self.VariationLabel.mas_left).**with**.offset(30);**//更改的是此處的width變?yōu)閣ith叁扫,否則會報錯**
make.**width**.equalTo(@60);**//此處的width不需要改動**
make.height.mas_equalTo(@30);
}];
注意:解決方法將width更改為with即可。并不是全部的width都要改變畜埋,注意看上面的代碼部分莫绣。
二.錯誤信息統(tǒng)計(父子控件之間的關系沒有建立好)
reason:couldn't find a common superview for<UIView: ...frame: ...layer: ...>
解決方法:查---好自己做約束的父子控件之間的關系是否建立起來了。
UITextField *nameTextField = [UITextField new];
nameTextField.font = [UIFont systemFontOfSize:14];
nameTextField.placeholder = @"請再次輸入密碼";
*** //父子控件的建立好關系:self.testView為父控件悠鞍,nameTextField為子控件***
[self.testView **addSubview**:nameTextField];
***//開始約束***
[lable mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.testView.mas_left).with.offset(20);
make.top.mas_equalTo(self.testView.mas_top).with.mas_offset(0);
make.height.mas_equalTo(30);
make.width.mas_equalTo(50);
}];
可能出現(xiàn)的錯誤章節(jié)摘于作者 Xcode8