简体   繁体   English

动态 UICollectionView 单元格高度

[英]Dynamic UICollectionView Cell Height

I have an array of stack views with varying heights: [Stack View A, Stack View B, Stack View C, Stack View D]我有一组不同高度的[Stack View A, Stack View B, Stack View C, Stack View D][Stack View A, Stack View B, Stack View C, Stack View D]

I want to display them in a collectionView .我想在collectionView显示它们。 Every stackView goes into a different collectionView cell .每个stackView进入不同的collectionView cell I am currently using a flow layout to set the height based on what the view is in the collection viewController .我目前正在使用流布局根据集合viewController的视图设置高度。

Would it be possible to abstract this so that the collection vc doesn't need to know anything about them?是否可以将其抽象化,以便集合 vc 不需要了解有关它们的任何信息? I can pass the view to be displayed in the cell and the cell's height is determined by the height of the stack view.我可以传递要在单元格中显示的视图,并且单元格的高度由堆栈视图的高度决定。 Something like intrinsic content size, as opposed to setting it in the flow layout delegate sizeforitem method.类似于内在内容大小,而不是在流布局委托sizeforitem方法中设置它。

You could set the Estimated Size of the collectionView as "Automatic" as well as the cell's size and each cell will self size with Auto Layout constrained views.您可以将 collectionView 的估计大小以及单元格的大小设置为“自动”,并且每个单元格都将使用自动布局约束视图自行调整大小。

As stated in the Xcode 11 Release Notes:如 Xcode 11 发行说明中所述:

Cells in a UICollectionView can now self size with Auto Layout constrained views in the canvas. UICollectionView 中的单元格现在可以使用画布中的自动布局约束视图自行调整大小。 To opt into the behavior for existing collection views, enable “Automatic” for the collection view's estimated size, and “Automatic” for cell's size from the Size inspector.要选择现有集合视图的行为,请在大小检查器中为集合视图的估计大小启用“自动”,并为单元格大小启用“自动”。 If deploying before iOS 13, you can activate self sizing collection view cells by calling performBatchUpdates(_:completion:) during viewDidLoad().如果在 iOS 13 之前部署,您可以通过在 viewDidLoad() 期间调用 performBatchUpdates(_:completion:) 来激活自调整集合视图单元格。 (45617083) (45617083)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM