iOS Masonry 更新要點(diǎn)
1.更新方法
AutoLayout方法 |
方法說(shuō)明 |
setNeedsLayout |
告知頁(yè)面需要更新纸肉,不會(huì)立即更新,執(zhí)行后會(huì)調(diào)用,loyoutSubviews |
layoutIfNeeded |
告知頁(yè)面布局立即更新翘瓮,一般會(huì)和setNeedsLayout一起使用 |
layoutSubviews |
系統(tǒng)重寫(xiě)布局 |
setNeedsUpdateConstraints |
告知需要更新約束器贩,不會(huì)立即執(zhí)行 |
updateConstraintsIfNeeded |
告知立刻更新約束 |
updateConstraints |
系統(tǒng)更新約束 |
2.設(shè)置全局 宏
-
define MAS_SHORTHAND_GLOBALS 使用全局宏定義,可以使equalTo等效于mas_equalTo
-
define MAS_SHORTHAND 使用全局宏定義, 可以在調(diào)用masonry方法的時(shí)候不使用mas_前綴
3.基本使用
- mas_makeConstraints:添加約束
- mas_updateConstraints:更新約束宠漩、亦可添加新約束
- mas_remakeConstraints:重置之前的約束
4.重要屬性
- multipler屬性表示約束值為約束對(duì)象的乘因數(shù)
- dividedBy屬性表示約束值為約束對(duì)象的除因數(shù),可用于設(shè)置view的寬高比
- priorityLow()設(shè)置約束優(yōu)先級(jí)
- baseline NSLayoutAttributeBaseline 文本基線
[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);
}];
5.新方法: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的類(lèi)擴(kuò)展:
// 創(chuàng)建水平排列圖標(biāo) arr中放置了2個(gè)或連個(gè)以上的初始化后的控件
// alongAxis 軸線方向 固定間隔 頭部間隔 尾部間隔
[arr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:20 leadSpacing:5 tailSpacing:5];
[arr makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@60);
make.height.equalTo(@60);
}];
6.使用動(dòng)畫(huà)更新約束
如果想要約束變換之后實(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];
}];
7.多行Label 適配
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];
}
8.scrollview適配
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的大小
9.自動(dòng)計(jì)算UITableviewCell高度
推薦使用一個(gè)庫(kù)UITableView-FDTemplateLayoutCell
想法:動(dòng)態(tài)高度的cell, 主要關(guān)注的點(diǎn)是什么肤寝?
viewController本身不需要知道cell的類(lèi)型
cell的高度與viewController沒(méi)有相關(guān)性,cell的高度由cell本身來(lái)決定
viewController真正做到的是一個(gè)
以下是摘抄過(guò)來(lái)的
主要是UILabel的高度會(huì)有變化抖僵,所以這里主要是說(shuō)說(shuō)label變化時(shí)如何處理鲤看,設(shè)置UILabel的時(shí)候注意要設(shè)置 preferredMaxLayoutWidth這個(gè)寬度,還有ContentHuggingPriority為 UILayoutPriorityRequried
CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width - 10 * 2;
textLabel = [UILabel new];
textLabel.numberOfLines = 0;
textLabel.preferredMaxLayoutWidth = maxWidth;
[self.contentView addSubview:textLabel];
[textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(statusView.mas_bottom).with.offset(10);
make.left.equalTo(self.contentView).with.offset(10);
make.right.equalTo(self.contentView).with.offset(-10);
make.bottom.equalTo(self.contentView).with.offset(-10);
}];
[_contentLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
如果版本支持最低版本為iOS 8以上的話可以直接利用UITableViewAutomaticDimension在tableview的heightForRowAtIndexPath直接返回即可耍群。
tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = 80; //減少第一次計(jì)算量义桂,iOS7后支持
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// 只用返回這個(gè)!
return UITableViewAutomaticDimension;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者