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

时间:2021-12-24 08:16:14

There's been a lot of questions about how to make dynamic cell height using Autolayout and TextView inside it. Here's the story

关于如何使用Autolayout和TextView制作动态单元格高度存在很多问题。这是故事

  1. I follow this article iOS dynamic table view cells with varying row height and Autolayout. In this case, we replace the 2nd label in the article with a TextView, with the same set of constraints

    我按照这篇文章iOS动态表格查看具有不同行高和Autolayout的单元格。在这种情况下,我们使用TextView替换文章中的第二个标签,并使用相同的约束集

  2. The TextView does not have intrinsic content size as the Label. So we must use sizeThatFits and creating height constraint on the TextView, like this.

    TextView没有标签的内在内容大小。所以我们必须使用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
  2. 然后我在控制台中看到了

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) ( "", "", "", "", "" )

弄清楚你没想到的; (2)找到添加了不需要的约束或约束的代码并修复它。 (注意:如果你看到你不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性的文档translatesAutoresizingMaskIntoConstraints)(“”,“”,“”,“”,“”)

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?

我错过了什么?

1 个解决方案

#1


Simply lowering the priority of the textViewHeightConstraint (below 1000) fixes the Unable to simultaneously satisfy constraints problem

简单地降低textViewHeightConstraint(低于1000)的优先级可以修复Unable同时满足约束问题

#1


Simply lowering the priority of the textViewHeightConstraint (below 1000) fixes the Unable to simultaneously satisfy constraints problem

简单地降低textViewHeightConstraint(低于1000)的优先级可以修复Unable同时满足约束问题