简体   繁体   English

如何更改表格视图单元格内的textview高度约束?

[英]How to change textview height constraint within table view cell?

There's been a lot of questions about how to make dynamic cell height using Autolayout and TextView inside it. 关于如何在其中使用Autolayout和TextView设置动态单元格高度存在很多问题。 Here's the story 这是故事

  1. I follow this article iOS dynamic table view cells with varying row height and Autolayout . 我关注这篇文章,其中的iOS动态表格视图单元格具有不同的行高和自动布局 In this case, we replace the 2nd label in the article with a TextView, with the same set of constraints 在这种情况下,我们使用具有相同约束集的TextView替换文章中的第二个标签

  2. The TextView does not have intrinsic content size as the Label. TextView没有固有的内容大小作为Label。 So we must use sizeThatFits and creating height constraint on the TextView, like this. 因此,我们必须使用sizeThatFits并在TextView上创建高度约束,如下所示。

This height constraint is an IBOutlet from the 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;
}
  1. I then see in the console that 然后我在控制台中看到
 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. (2)查找添加了一个或多个不必要约束的代码并进行修复。 (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "", "", "", "", "" ) (注意:如果看到的是您不了解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性translationsAutoresizingMaskIntoConstraints的文档)(“”,“”,“”,“”,“”)

 Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7fa3e3988a90 UITextView:0x7fa3e210f000.height == 200> 

Here you can see that the Autolayout system removes the height constraint on the TextView. 在这里您可以看到Autolayout系统删除了TextView上的高度限制。

The problem here is that we update the height constraint on the TextView, but the contentView of the cell seems to ignore this. 这里的问题是我们更新了TextView的高度限制,但是单元格的contentView似乎忽略了这一点。

I know the key to this dynamic height is that the subviews (Label, TextView) must determine its own size (Label has its own intrinsic content size, for TextView we manually set its height constraint) so that the contentSize is then calculated 我知道此动态高度的关键在于子视图(Label,TextView)必须确定其自身的大小(Label具有其自身的固有内容大小,对于TextView我们手动设置其高度约束),以便随后计算contentSize

What am I missing? 我想念什么?

只需降低textViewHeightConstraint的优先级(低于1000)即可解决Unable to simultaneously satisfy constraints问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM