如何更改 table 视图单元格内的 textview 高度限制?
How to change textview height constraint within table view cell?
关于如何在其中使用 Autolayout 和 TextView 制作动态单元格高度的问题很多。这是故事
我关注这篇文章iOS dynamic table view cells with varying row height and Autolayout。在这种情况下,我们将文章中的第二个标签替换为具有相同约束集的 TextView
TextView 没有与 Label 一样的固有内容大小。所以我们必须使用 sizeThatFits
并在 TextView 上创建高度约束,就像这样。
这个高度约束是来自 Nib 的 IBOutlet
ViewController.m
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Item *item = self.dataSource.items[indexPath.row];
[self.prototypeCell configureWithModel:item];
[self.prototypeCell updateConstraintsIfNeeded];
[self.prototypeCell layoutIfNeeded];
self.prototypeCell.textViewHeightConstraint.constant = [self.prototypeCell.textView sizeThatFits:CGSizeMake(self.prototypeCell.frame.size.width, CGFLOAT_MAX)].height;
[self.prototypeCell updateConstraintsIfNeeded];
[self.prototypeCell layoutIfNeeded];
return [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
}
CustomCell.m
- (void)configureWithModel:(Item *)model {
self.textView.text = model.content;
}
- 然后我在控制台中看到
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. (Note: If you're seeing
NSAutoresizingMaskLayoutConstraints that you don't understand, refer
to the documentation for the UIView property
translatesAutoresizingMaskIntoConstraints)
(
"",
"",
"",
"",
""
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fa3e3988a90 UITextView:0x7fa3e210f000.height == 200>
这里可以看到Autolayout系统去掉了TextView的高度限制。
这里的问题是我们更新了TextView上的高度约束,但是cell的contentView
似乎忽略了这个。
我知道这个动态高度的关键是子视图(Label,TextView)必须确定自己的大小(Label 有自己的固有内容大小,对于 TextView 我们手动设置它的高度约束)这样 [=然后计算 19=]
我错过了什么?
只需降低 textViewHeightConstraint
的优先级(低于 1000)即可解决 Unable to simultaneously satisfy constraints
问题
关于如何在其中使用 Autolayout 和 TextView 制作动态单元格高度的问题很多。这是故事
我关注这篇文章iOS dynamic table view cells with varying row height and Autolayout。在这种情况下,我们将文章中的第二个标签替换为具有相同约束集的 TextView
TextView 没有与 Label 一样的固有内容大小。所以我们必须使用
sizeThatFits
并在 TextView 上创建高度约束,就像这样。
这个高度约束是来自 Nib 的 IBOutlet
ViewController.m
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Item *item = self.dataSource.items[indexPath.row];
[self.prototypeCell configureWithModel:item];
[self.prototypeCell updateConstraintsIfNeeded];
[self.prototypeCell layoutIfNeeded];
self.prototypeCell.textViewHeightConstraint.constant = [self.prototypeCell.textView sizeThatFits:CGSizeMake(self.prototypeCell.frame.size.width, CGFLOAT_MAX)].height;
[self.prototypeCell updateConstraintsIfNeeded];
[self.prototypeCell layoutIfNeeded];
return [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
}
CustomCell.m
- (void)configureWithModel:(Item *)model {
self.textView.text = model.content;
}
- 然后我在控制台中看到
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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "", "", "", "", "" )
Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7fa3e3988a90 UITextView:0x7fa3e210f000.height == 200>
这里可以看到Autolayout系统去掉了TextView的高度限制。
这里的问题是我们更新了TextView上的高度约束,但是cell的contentView
似乎忽略了这个。
我知道这个动态高度的关键是子视图(Label,TextView)必须确定自己的大小(Label 有自己的固有内容大小,对于 TextView 我们手动设置它的高度约束)这样 [=然后计算 19=]
我错过了什么?
只需降低 textViewHeightConstraint
的优先级(低于 1000)即可解决 Unable to simultaneously satisfy constraints
问题