http://www.lxweimin.com/p/yuCytg
QQ.png
兩個重要概念:約束、參照
- 約束:二者之間所固定的相對位置
- 參照:指的是約束是相對于誰來說的
Masonry常用方法
-
mas_makeConstraints
創建constraint后,你可以使用局部變量或屬性,來保存以便下次引用它;如果創建多個constraints,可以采用數組來保存它們
-(NSArray*)mas_makeConstraints:(void(^)(MASConstraintMaker*))block {
}
-
mas_updateConstraints
有時你需要更新constraint(例如,動畫和調試),而不是創建固定constraint,可以使用mas_updateConstraints
方法。
-(NSArray*)mas_updateConstraints:(void(^)(MASConstraintMaker*))block {
}
-
mas_remakeConstraints
與mas_updateConstraints
相似,都是更新constraint。不過,mas_remakeConstraints
是刪除之前constraint,然后再添加新的constraint(適用于移動動畫);而mas_updateConstraints
只是更新constraint的值。
-(NSArray*)mas_remakeConstraints:(void(^)(MASConstraintMaker*make))block {
}
使用舉例
make.top.equalTo(self.MoveBtn.mas_bottom); //不帶間距
make.top.left.equalTo(self.view);
make.top.left.right.bottom.equalTo(self.view);
make.left.equalTo(self.MoveBtn.mas_right).offset(10); //帶間距
make.top.left.equalTo(self.view).offset(10);
make.width.equalTo(self.MoveBtn.mas_width); //等寬
make.height.equalTo(self.MoveBtn.mas_height); //等高
make.width.height.equalTo(self.MoveBtn); //等寬等高
make.width.equalTo(@30); //直接設置寬度
make.width.height.equalTo(@30); //直接設置寬高
make.width.equalTo(self.view.mas_width).multipliedBy(0.6); //0.6倍
make.height.equalTo(self.view.mas_height).dividedBy(2); //除以2
make.center.equalTo(self.MoveBtn.mas_center); //同中心
make.centerX.equalTo(self.MoveBtn.mas_center_X); //中心x對齊
make.centerY.equalTo(self.contentView.mas_centerY); //中心y對齊
建議:最好按上左下右的順序設置。
Masonry布局小技巧
- 等大的子控件,等間距分布
http://www.lxweimin.com/p/2c7a5ba73fc2
NSInteger padding = 0;
[self.btnArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:padding leadSpacing:padding tailSpacing:padding];
[self.btnArray mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self).offset(padding);
make.height.equalTo(self);
make.width.equalTo(self).dividedBy(arr.count);
}];
- 要拿到子控件的frame
布局完,執行
[aLabel.superview layoutIfNeeded];
參考http://www.lxweimin.com/p/ad9c075a7547
設置尺寸
make.size.mas_equalTo(CGSizeMake(300, 300));
設置內邊距
[_mainImgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(UIEdgeInsetsMake(10, 10, 10, 10));
}];
或
make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
- 大于等于
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
// 設置寬度 小于等于200
make.width.lessThanOrEqualTo(@200);
// 設置高度 大于等于10
make.height.greaterThanOrEqualTo(@(10));
}];
優先級
每個約束(如top
)都有一個優先級,范圍是1-1000,創建的約束優先級默認是最高的1000。
優先級為了解決:有些場景需要動態進行布局,如果存在多套約束的情況下,解決約束沖突的問題。
如,其中一個控件view1會動態刪除。另一個控件view2設約束時的參照物是view1,那view就需要設置兩套約束,并添加優先級。當參照物發生變化,改用優先級低的約束。
_v1 = [[UIView alloc] init];
_v1.backgroundColor = [UIColor redColor];
[self.view addSubview:_v1];
[_v1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.equalTo(self.view).offset(100);
make.width.height.equalTo(@80);
}];
_v2 = [[UIView alloc] init];
_v2.backgroundColor = [UIColor greenColor];
[self.view addSubview:_v2];
[_v2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(100);
make.top.equalTo(_v1.mas_bottom).offset(25);
make.width.height.equalTo(@80);
}];
UIView *v3 = [[UIView alloc] init];
v3.backgroundColor = [UIColor purpleColor];
[self.view addSubview:v3];
[v3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(100);
// 對同種約束,設置不同優先級
make.top.equalTo(_v2.mas_bottom).offset(25).priority(1000);
make.top.equalTo(_v1.mas_bottom).offset(25).priority(999);
make.width.height.mas_equalTo(_v2).priority(1000);
make.width.height.mas_equalTo(@120).priority(999);
}];
效果:調整優先級后,默認是優先級最高的那個生效,然后刪除_v2后,備用的約束就會生效了。
固有約束
有些控件能通過自己顯示的內容,自動計算出需要的size。這個size就叫該控件的固有內容大小。這個大小是和需要顯示的內容相關的。UIButton,UILabel
就是具有固有內容大小屬性的控件。
Content Hugging Priority:
該優先級表示一個控件抗被拉伸的優先級。優先級越高,越難被拉伸,默認是251。
Content Compression Resistance Priority:
該優先級 表示一個控件抗壓縮的優先級。優先級越高,越難被壓縮,默認是750。
例子:如一個Label,設置了的左右間距是100,設置的text字數多的話就被省略了。
-
這是因為,左右約束的優先級,比固有內容相關的優先級要高,所以Autolayout會優先滿足左右兩個約束,而屏幕寬度有限,最后只能壓縮Label顯示的寬度。這時候Label是被壓縮的。
-
我們可以改變控件的抗壓縮優先級
-
總之,對同一個約束效果,誰的優先級高,就按高低順序先滿足誰的約束效果。
應用:兩個Label的遮蓋問題。可以通過改變控件的抗壓縮優先級來解決。 把抗壓縮優先級低的Label,壓縮掉。
-
- Masonry設置優先級
[leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(10);
make.centerY.equalTo(self.view);
make.right.mas_lessThanOrEqualTo(rightLabel.mas_left);
}];
[rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_greaterThanOrEqualTo(leftLabel.mas_right);
make.right.equalTo(self.view).offset(-10);
make.centerY.equalTo(leftLabel);
}];
- 代碼設置
[leftLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
或
[rightLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
UILayoutPriority
類型實際上就是float
,只要設置右邊的比左邊的大就可以。
UIScrollView與Masonry
事先并不知道UIScrollView的contentSize的大小。內部視圖有多少contentSize就自動擴充到多大。
這種方式的實現,主要是依賴于創建一個containerView內容視圖,并添加到UIScrollView上作為子視圖。UIScrollView原來的子視圖都添加到containerView上,并且和這個視圖設置約束。
因為對UIScrollView進行addSubview操作的時候,本質上是往其contentView上添加。也就是containerView的父視圖是contentView,通過containerView撐起contentView視圖的大小,以此來實現動態改變contentSize。
// 在進行約束的時候,要對containerView的上下左右都添加和子視圖的約束,以便確認containerView的邊界區域。
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
CGFloat padding = 10;
[self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(_scrollView).insets(UIEdgeInsetsMake(padding, padding, padding, padding));
}];
// self.containerView添加子控件并布局。
// ... ...