Custom UITableViewCell中忽略Autolayout [英] Autolayout is ignored in Custom UITableViewCell

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

问题描述

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

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的单元格,但也忽略了Autolayout。

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)

  • 底部空间到容器边距以确保单元格具有所有垂直约束以确定其高度

  • Leading space to superview
  • Top space to superview
  • Fixed width and height (100 & 100)
  • Bottom space to container margin to make sure that the cell has all vertical constraints to determine its height

并简化了此界面,自动布局仍然没有考虑在内,这暗示我的问题不是来自设置不当的约束。

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.

这是一个简单的错误,请参阅下面的答案。

This was a simple mistake, see my answer below.

推荐答案

自动布局是被忽略,因为原型单元格和单元格的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.

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

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