簡(jiǎn)要
自動(dòng)布局最重要的是約束:UI元素間關(guān)系的數(shù)學(xué)表達(dá)式。約束包括尺寸、由優(yōu)先級(jí)和閾值管理的相對(duì)位置。它們是添加劑,可能導(dǎo)致約束沖突 、約束不足造成布局無(wú)法確定 。這兩種情況都會(huì)產(chǎn)生異常。
使用前:AutoLayout關(guān)于更新的幾個(gè)方法的區(qū)別
-
setNeedsLayout:告知頁(yè)面需要更新,但是不會(huì)立刻開(kāi)始更新。執(zhí)行后會(huì)立刻調(diào)用layoutSubviews。 -
layoutIfNeeded:告知頁(yè)面布局立刻更新。所以一般都會(huì)和setNeedsLayout一起使用。如果希望立刻生成新的frame需要調(diào)用此方法,利用這點(diǎn)一般布局動(dòng)畫(huà)可以在更新布局后直接使用這個(gè)方法讓動(dòng)畫(huà)生效。 -
layoutSubviews:系統(tǒng)重寫(xiě)布局 -
setNeedsUpdateConstraints:告知需要更新約束,但是不會(huì)立刻開(kāi)始 -
updateConstraintsIfNeeded:告知立刻更新約束 -
updateConstraints:系統(tǒng)更新約束
使用
1. 基本使用
mas_makeConstraints:添加約束mas_updateConstraints:更新約束、亦可添加新約束mas_remakeConstraints:重置之前的約束multipler屬性表示約束值為約束對(duì)象的乘因數(shù),dividedBy屬性表示約束值為約束對(duì)象的除因數(shù),可用于設(shè)置view的寬高比
// 進(jìn)行屏幕的適配的時(shí)候,往往需要根據(jù)屏幕寬度來(lái)適配一個(gè)相應(yīng)的高度,在此推薦使用如下約束的方式來(lái)進(jìn)行控件的適配
[self.topView addSubview:self.topInnerView];
[self.topInnerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(self.topView.mas_height).dividedBy(3);
make.width.and.height.lessThanOrEqualTo(self.topView);
make.width.and.height.equalTo(self.topView).with.priorityLow();
make.center.equalTo(self.topView);
}];
-
priorityLow()設(shè)置約束優(yōu)先級(jí) -
#define MAS_SHORTHAND_GLOBALS使用全局宏定義,可以使equalTo等效于mas_equalTo -
#define MAS_SHORTHAND使用全局宏定義, 可以在調(diào)用masonry方法的時(shí)候不使用mas_前綴
// 這里注意到一個(gè)地方,就是當(dāng)使用了這個(gè)全局宏定義之后,發(fā)現(xiàn)可以有個(gè)類`NSArray+MASAdditions.h`,看了之后發(fā)現(xiàn)可以
self.buttonViews = @[ raiseButton, lowerButton, centerButton ];
// 之后可以在updateConstraints 方法中
- (void)updateConstraints {
[self.buttonViews updateConstraints:^(MASConstraintMaker *make) {
make.baseline.equalTo(self.mas_centerY).with.offset(self.offset);
}];
[super updateConstraints];
}
- 動(dòng)態(tài)修改視圖約束:
// 創(chuàng)建視圖約束
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
self.animatableConstraint = make.edges.equalTo(superview).insets(paddingInsets).priorityLow();
]];
// 更改約束 (另一處方法中)
UIEdgeInsets paddingInsets = UIEdgeInsetsMake(padding, padding, padding, padding);
self.animatableConstraint.insets = paddingInsets;
[self layoutIfNeeded];
-
debug模式:
// 對(duì)某個(gè)view添加key值
greenView.mas_key = @"greenView";
// 或者如下順序
MASAttachKeys(greenView, redView, blueView, superview);
// 同樣的對(duì)每條約束亦可以添加key
make.height.greaterThanOrEqualTo(@5000).key(@"ConstantConstraint");
-
preferredMaxLayoutWidth: 多行l(wèi)abel的約束問(wèn)題
// 已經(jīng)確認(rèn)好了位置
// 在layoutSubviews中確認(rèn)label的preferredMaxLayoutWidth值
- (void)layoutSubviews {
[super layoutSubviews];
// 你必須在 [super layoutSubviews] 調(diào)用之后,longLabel的frame有值之后設(shè)置preferredMaxLayoutWidth
self.longLabel.preferredMaxLayoutWidth = self.frame.size.width-100;
// 設(shè)置preferredLayoutWidth后,需要重新布局
[super layoutSubviews];
}
-
scrollView使用約束的問(wèn)題:原理通過(guò)一個(gè)contentView來(lái)約束scrollView的contentSize大小,也就是說(shuō)以子控件的約束條件,來(lái)控制父視圖的大小
// 1. 控制scrollView大?。@示區(qū)域)
[self.scrollView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
// 2. 添加一個(gè)contentView到scrollView,并且添加好約束條件
[contentView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.scrollView);
// 注意到此處的寬度約束條件,這個(gè)寬度的約束條件是比添加項(xiàng)
make.width.equalTo(self.scrollView);
}];
// 3. 對(duì)contentView的子控件做好約束,達(dá)到可以控制contentView的大小
- 新方法:2個(gè)或2個(gè)以上的控件等間隔排序
/**
* 多個(gè)控件固定間隔的等間隔排列,變化的是控件的長(zhǎng)度或者寬度值
*
* @param axisType 軸線方向
* @param fixedSpacing 間隔大小
* @param leadSpacing 頭部間隔
* @param tailSpacing 尾部間隔
*/
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType
withFixedSpacing:(CGFloat)fixedSpacing l
eadSpacing:(CGFloat)leadSpacing
tailSpacing:(CGFloat)tailSpacing;
/**
* 多個(gè)固定大小的控件的等間隔排列,變化的是間隔的空隙
*
* @param axisType 軸線方向
* @param fixedItemLength 每個(gè)控件的固定長(zhǎng)度或者寬度值
* @param leadSpacing 頭部間隔
* @param tailSpacing 尾部間隔
*/
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType
withFixedItemLength:(CGFloat)fixedItemLength
leadSpacing:(CGFloat)leadSpacing
tailSpacing:(CGFloat)tailSpacing;
使用方法很簡(jiǎn)單,因?yàn)樗荖SArray的類擴(kuò)展:
// 創(chuàng)建水平排列圖標(biāo) arr中放置了2個(gè)或連個(gè)以上的初始化后的控件
// alongAxis 軸線方向 固定間隔 頭部間隔 尾部間隔
[arr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:20 leadSpacing:5 tailSpacing:5];
[arr mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@60);
make.height.equalTo(@60);
}];
2. 注意事項(xiàng)
- 約束視圖對(duì)象只有在被
addSubview之后,才能給視圖添加約束 - 當(dāng)你的所有約束都在
updateConstraints內(nèi)調(diào)用的時(shí)候,你就需要在此調(diào)用此方法,因?yàn)?updateConstraints方法是需要觸發(fā)的
// 調(diào)用在view 內(nèi)部,而不是viewcontroller
+ (BOOL)requiresConstraintBasedLayout {
return YES;
}
/**
* 蘋(píng)果推薦 約束 增加和修改 放在此方法種
*/
- (void)updateConstraints {
[self.growingButton updateConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self);
make.width.equalTo(@(self.buttonSize.width)).priorityLow();
make.height.equalTo(@(self.buttonSize.height)).priorityLow();
make.width.lessThanOrEqualTo(self);
make.height.lessThanOrEqualTo(self);
}];
//最后記得回調(diào)super方法
[super updateConstraints];
}
- 如果想要約束變換之后實(shí)現(xiàn)動(dòng)畫(huà)效果,則需要執(zhí)行如下操作
// 通知需要更新約束,但是不立即執(zhí)行
[self setNeedsUpdateConstraints];
// 立即更新約束,以執(zhí)行動(dòng)態(tài)變換
// update constraints now so we can animate the change
[self updateConstraintsIfNeeded];
// 執(zhí)行動(dòng)畫(huà)效果, 設(shè)置動(dòng)畫(huà)時(shí)間
[UIView animateWithDuration:0.4 animations:^{
[self layoutIfNeeded];
}];