一螃诅、高度自適應(yīng)
高度自適應(yīng)這個功能比較簡單了枷踏,滿足下面三點即可:
-
1菩暗、設(shè)置
UITableView
的rowHeight
為UITableViewAutomaticDimension
,如:_tableView.rowHeight = UITableViewAutomaticDimension;/// 高度自適應(yīng)
-
2旭蠕、移除
UITableViewDelegate
的代理heightForRowAtIndexPath
停团,就是下面的代碼不要出現(xiàn)在頁面內(nèi)。- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ }
-
3掏熬、設(shè)置好
UITableViewCell
內(nèi)的控件約束佑稠。如:UILabel *label = [[UILabel alloc] init]; [self.contentView addSubview:label]; [label mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.contentView).offset(8); make.left.mas_equalTo(self.contentView).offset(12); make.right.mas_equalTo(self.contentView).offset(-12); make.height.mas_greaterThanOrEqualTo(20).priorityHigh(); make.bottom.mas_equalTo(self.contentView).offset(-16); }];
二、遇到的問題 & 解決方法
1旗芬、約束警告
[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
****
)
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
上面的問題是由于系統(tǒng)判定代碼和編輯器中可能出現(xiàn)了重復(fù)約束舌胶,可以不做處理,跳過疮丛!解決辦法就是給有問題的約束**設(shè)置優(yōu)先級**
幔嫂。
為什么會出現(xiàn)重復(fù)約束呢?如第一步中第三點誊薄,關(guān)于label的約束履恩,正常情況下設(shè)置top
、left
呢蔫、bottom
似袁、right
已經(jīng)可以確定一個控件的位置了,但是這里還設(shè)置了height
的約束,這個height
就是重復(fù)約束昙衅,給其設(shè)定優(yōu)先級priorityHigh()
即可扬霜。
2、UILabel內(nèi)容展示不全
需求是完整展示多行文本
而涉。代碼按常規(guī)方式寫完著瓶,但實際測試結(jié)果卻是在部分機(jī)型上,內(nèi)容展示不全啼县,顯示...
材原。
解決方法:在設(shè)置完font
、numberOfLines
之后季眷,再設(shè)置adjustsFontSizeToFitWidth
余蟹。
但是在UITableView
和UITableViewCell
上的控件,設(shè)置完adjustsFontSizeToFitWidth
之后子刮,發(fā)現(xiàn)內(nèi)容還是顯示不全威酒。這個時候需要額外設(shè)置preferredMaxLayoutWidth
才能解決這個問題。
最終的效果就是:
UILabel *label = [[UILabel alloc] init];
label.font = PFFont_Regular(14);
label.numberOfLines = 0;
label.preferredMaxLayoutWidth = 200;
label.adjustsFontSizeToFitWidth = YES;
[self.contentView addSubview:label];
3挺峡、多個控件同時約束bottom
這里以簡單情況為例:
效果一 | 效果二 |
---|---|
如上面表格所示葵孤,由于UILabel
內(nèi)容的不確定性,UITableViewCell
的高度需要根據(jù)多個控件來確定橱赠,可能是第一個控件尤仍,也可能是第二個控件。做法也比較簡單給兩個控件分別添加bottom
約束狭姨。
/// 內(nèi)容1
UILabel *label1 = [[UILabel alloc] init];
label1.font = PFFont_Regular(14);
label1.numberOfLines = 0;
[self.contentView addSubview:label1];
[label1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.contentView.mas_bottom).offset(15);
make.left.mas_equalTo(self.contentView).offset(15);
make.width.mas_equalTo(100);
make.height.mas_greaterThanOrEqualTo(20).priorityHigh();
make.bottom.mas_equalTo(self.contentView).offset(-15);
}];
/// 內(nèi)容2
UILabel *label2 = [[UILabel alloc] init];
label2.font = PFFont_Regular(14);
label2.numberOfLines = 0;
[self.contentView addSubview:label2];
[label2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.contentView.mas_bottom).offset(15);
make.right.mas_equalTo(self.contentView).offset(-15);
make.width.mas_equalTo(100);
make.height.mas_greaterThanOrEqualTo(20).priorityHigh();
make.bottom.mas_equalTo(self.contentView).offset(-15);
}];
但是在實際情況下宰啦,上面情況中兩個UILabel
的高度其實是一致的,這種情況下我的解決辦法是給Label文本居頂
,具體做法參考文章