【发布时间】:2018-01-03 09:55:14
【问题描述】:
我有一个UITableView,其中包含一个单元格:
Title label + UIStackView 带有一些按钮,在其下方是 UIStackView,带有另一个标签(描述)、UICollectionView(一组图像)和一个按钮。标题标签固定到前导、尾随和顶部超级视图,底部固定到UIStackView(固定到前导、尾随、标签底部和超级视图底部)。
我也在使用UITableViewAutomaticDimension,这是因为UIStackView(图像、文本)内部的内容高度可能不同。
在我将UICollectionView 添加到UIStackView 之前,一切正常,但在某些情况下添加它后布局会中断(UIStackView 中的所有内容都重叠)并且在 View UI Hierarchy 中我可以看到警告:
*UIView:0x1038635e0- AMBIGUOUS LAYOUT for UIView:0x1038635e0.Height{id: 3153}
Legend:
* - is laid out with auto layout
+ - is laid out manually, but is represented in the layout engine because translatesAutoresizingMaskIntoConstraints = YES
• - layout engine host
2018-01-03 10:19:58.527094+0100 Crowd Knowledge[1462:1408352] [LayoutConstraints] View has an ambiguous layout. See "Auto Layout Guide: Ambiguous Layouts" for help debugging. Displaying synopsis from invoking -[UIView _autolayoutTrace] to provide additional detail.
*Crowd_Knowledge.AttachmentsCollectionView:0x104072e00- AMBIGUOUS LAYOUT for Crowd_Knowledge.AttachmentsCollectionView:0x104072e00.minY{id: 3263}, Crowd_Knowledge.AttachmentsCollectionView:0x104072e00.Height{id: 3252}
Legend:
* - is laid out with auto layout
+ - is laid out manually, but is represented in the layout engine because translatesAutoresizingMaskIntoConstraints = YES
• - layout engine host
2018-01-03 10:19:58.531696+0100 Crowd Knowledge[1462:1408352] [LayoutConstraints] View has an ambiguous layout. See "Auto Layout Guide: Ambiguous Layouts" for help debugging. Displaying synopsis from invoking -[UIView _autolayoutTrace] to provide additional detail.
*UIButton:0x103854dc0'Edit solution'- AMBIGUOUS LAYOUT for UIButton:0x103854dc0'Edit solution'.minY{id: 3261}, UIButton:0x103854dc0'Edit solution'.Height{id: 3258}
Legend:
* - is laid out with auto layout
+ - is laid out manually, but is represented in the layout engine because translatesAutoresizingMaskIntoConstraints = YES
• - layout engine host
2018-01-03 10:19:59.247887+0100 Crowd Knowledge[1462:1408352] [LayoutConstraints] Window has a view with an ambiguous layout. See "Auto Layout Guide: Ambiguous Layouts" for help debugging. Displaying synopsis from invoking -[UIView _autolayoutTrace] to provide additional detail.
*UIView:0x1038635e0- AMBIGUOUS LAYOUT for UIView:0x1038635e0.Height{id: 3153}
Legend:
* - is laid out with auto layout
+ - is laid out manually, but is represented in the layout engine because translatesAutoresizingMaskIntoConstraints = YES
• - layout engine host
另外,我的UICollectionView 有一个布局,它根据内容设置高度:
class DynamicCollectionView: UICollectionView {
override func layoutSubviews() {
super.layoutSubviews()
if !__CGSizeEqualToSize(bounds.size, self.intrinsicContentSize) {
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
return contentSize
}
}
我在情节提要中没有任何警告,它在 90% 的时间里都有效。如果它坏了,我所要做的就是滚动另一个单元格,这样它就会刷新并且约束再次起作用。我尝试了各种技巧来强制更新 UI (tableView.beginUpdates() + tableView.endUpdates()),但我仍然找不到始终有效的解决方案。
我正在为这个问题寻找一个干净的解决方案。
【问题讨论】:
-
我猜您没有固定的 StackView 大小 - 高度约束。另外,请尝试为 stackView 中的所有元素添加固定大小。
-
我没有。 Description 和 UiCollectionView 具有动态内容 - 我无法设置固定大小。
-
您是否将stackview 和titlelabel 的
translateautoresizingmaskintoconstraints设置为false?!在您的日志中显示是 -
我试过了,但还是不行。问题是当 UICollectionView 加载图像列表并刷新自身时,有时它会由于某种原因破坏整个 UIStackView。它看起来不知道高度。
-
嗯。 stackview的分布是什么?是
.fillProportionately吗?可以改成fill吗?
标签: ios swift uitableview uicollectionview autolayout