如何更改表格视图单元格内的textview高度约束? [英] How to change textview height constraint within table view cell?

查看:126
本文介绍了如何更改表格视图单元格内的textview高度约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何使用其中的Autolayout和TextView来设置动态单元格高度存在很多问题.这是故事

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

  1. 我关注这篇文章 iOS动态表格视图单元格具有不同的行高和自动布局.在这种情况下,我们使用具有相同约束集的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

TextView没有固有的内容大小作为Label.因此,我们必须使用sizeThatFits并在TextView上创建高度约束,像这样.

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.

此高度限制是笔尖的IBOutlet

This height constraint is an IBOutlet from the Nib

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. 然后我在控制台中看到

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

找出您不希望看到的东西; (2)找到添加了 一个或多个不必要的约束并进行修复. (注意:如果您看到 您不了解的NSAutoresizingMaskLayoutConstraints,请参阅 到UIView属性的文档 AutoresizingMaskIntoConstraints) ( ", ", ", ", " )

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上的高度限制.

Here you can see that the Autolayout system removes the height constraint on the TextView.

这里的问题是我们更新了TextView的高度约束,但是单元格的contentView似乎忽略了这一点.

The problem here is that we update the height constraint on the TextView, but the contentView of the cell seems to ignore this.

我知道此动态高度的关键在于子视图(Label,TextView)必须确定其自身的大小(Label具有其自身的内在内容大小,对于TextView,我们手动设置其高度约束),以便contentSize为然后计算

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

我想念什么?

推荐答案

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

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

这篇关于如何更改表格视图单元格内的textview高度约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆