视图高度的模糊布局
Posted
技术标签:
【中文标题】视图高度的模糊布局【英文标题】:AMBIGUOUS LAYOUT for view Height 【发布时间】: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.Heightid: 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.minYid: 3263, Crowd_Knowledge.AttachmentsCollectionView:0x104072e00.Heightid: 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'.minYid: 3261, UIButton:0x103854dc0'Edit solution'.Heightid: 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.Heightid: 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
吗?
【参考方案1】:
“UIStackView(固定在父视图的前导、尾随和底部)”
为什么它没有固定在超级视图的顶部?您还必须将其固定到超级视图的顶部。不需要固定大小...
为了一个很好的演示。见this moment of this WWDC video。我强烈建议您观看整个视频。但从那一刻开始,您将了解很多有关您的问题的信息。
【讨论】:
因为它固定在标题标签的顶部(正如我所说的标题标签固定在 UIStackView 的底部)。它在大多数情况下都可以工作,但有时它会分开(在完全相同的内容上),但在滚动底部并返回顶部(因此必须刷新)之后,它又好了。 我不确定我是否理解你。 stackview 的底部固定在标签的顶部。这与 stackview 的顶部锚点有什么关系?! 不,标签底部固定在堆栈视图的顶部。以上是关于视图高度的模糊布局的主要内容,如果未能解决你的问题,请参考以下文章