在自定义 UITableViewCell 中忽略自动布局 [英] Autolayout is ignored in Custom UITableViewCell

查看:31
本文介绍了在自定义 UITableViewCell 中忽略自动布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管对所有元素设置了约束,包括单元格计算高度所需的垂直元素,自动布局似乎被忽略:所有单元格都被挤压.

Despite having set constraints to all elements, including the vertical ones needed for the cell to calculate its height, auto-layout seems to be ignored: all cells are squeezed.

这是结果和故事板中约束的屏幕截图:

Here's a screenshot of the result and of the constraints in the storyboard:

在保存tableView的VC中,这是viewDidLoad中的代码:

In the VC that holds the tableView, here's the code in viewDidLoad:

tableView.estimatedRowHeight = 120.0
tableView.rowHeight = UITableViewAutomaticDimension

注释掉第二行会得到高度为 120.0 的单元格,但自动布局也会被忽略.

Commenting out the second line gives cells with a height of 120.0 but Autolayout is ignored as well.

为了简化界面,我留下了一个标签,作为约束:

To simplify the interface, I've left a single label with, as constraints:

  • 领先的超级视图空间
  • 超级视图的顶部空间
  • 固定宽度和高度(100 和 100)
  • 底部空间到容器边距以确保单元格具有所有垂直约束来确定其高度

在这个简化的界面中,仍然没有考虑自动布局,这暗示我问题不是来自设置不当的约束.

And with this simplified interface, auto-layout is still not taken into account, which hints me that the problem did not come from badly set constraints.

在尺寸检查器中,行高设置为 120,并选中自定义.单元格具有正确的自定义类,单元格重用标识符正确.

In the Size Inspector, the row height is set on 120 and Custom is checked. The cell has the right custom class, the cell reuse identifier is correct.

推荐答案

自动布局被忽略,因为原型单元格和单元格的 UIView 都被赋予了 IB 中的自定义单元格类.

Auto-layout was ignored because both the prototype cell AND the UIView of the cell had been given the custom cell class in IB.

将 UIView 设置回 UIView 类解决了问题.

Setting the UIView back to UIView class solved the problem.

这篇关于在自定义 UITableViewCell 中忽略自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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