问题描述
我正在以编程方式使用自动布局约束来布局我的自定义 UITableView 单元格,并且我在 tableView:heightForRowAtIndexPath:
它在 iOS6 上运行良好,在 iOS7 上也看起来正常
但是当我在 iOS7 上运行应用程序时,我在控制台中看到的消息是这样的:
而且确实在那个列表中有一个我不想要的约束:
我无法将 contentView
的 translatesAutoresizingMaskIntoConstraints
属性设置为 NO => 它会弄乱整个单元格.
44 是默认单元格高度,但我在表格视图委托中定义了我的自定义高度,那么为什么单元格 contentView 有这个约束?什么可能导致这种情况?
在 iOS6 中没有发生这种情况,在 iOS6 和 iOS7 上一切看起来都很好.
我的代码比较大,这里就不贴了
在单元初始化时指定我的操作方式:
- 我创建了我所有的标签、按钮等
- 我将他们的
translatesAutoresizingMaskIntoConstraints
属性设置为 NO - 我将它们添加为单元格
contentView
的子视图 - 我在
contentView
上添加了约束
我也很想了解为什么这种情况只发生在 iOS7 上.
我也遇到了这个问题.. 似乎 contentView 的框架在调用 layoutSubviews
之前不会更新,但是框架单元格的 较早更新,在评估约束时将 contentView 的框架设置为 {0, 0, 320, 44}
.
更详细地查看 contentView 后,似乎不再设置 autoresizingMasks.
在约束视图之前设置 autoresizingMask 可以解决此问题:
Swift5、ios14:
I'm using auto layout constraints programmatically to layout my custom UITableView cells and I'm correctly defining the cell sizes in tableView:heightForRowAtIndexPath:
It's working just fine on iOS6 and it does look fine in iOS7 as well
BUT when I run the app on iOS7, here's the kind of message I see in the console:
And indeed there's one of the constraint in that list I don't want :
and I cannot set the translatesAutoresizingMaskIntoConstraints
property of the contentView
to NO => it would mess up the entire cell.
44 is the default cell height but I defined my custom heights in the table view delegate so why does the cell contentView has this constraint? What could cause this?
In iOS6 it's not happening and everything looks just fine on both iOS6 and iOS7.
My code is quite big so I won't post it here but feel free to ask for a pastebin if you need it.
To specify how I'm doing it, on cell intialization:
- I create all my labels, buttons, etc
- I set their
translatesAutoresizingMaskIntoConstraints
property to NO - I add them as subviews of the
contentView
of the cell - I add the constraints on the
contentView
I'm also deeply interested in understanding why this happens only on iOS7.
I had this problem as well.. It appears that the contentView's frame doesn't get updated until layoutSubviews
is called however the frame of the cell is updated earlier leaving the contentView's frame set to {0, 0, 320, 44}
at the time when the constraints are evaluated.
After looking at the contentView in more detail, It appears that autoresizingMasks are no longer being set.
Setting the autoresizingMask before you constrain your views can resolve this issue:
Swift5, ios14:
这篇关于UITableViewCell中iOS7上的自动布局约束问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!