动态 UICollectionView 单元格高度

Dynamic UICollectionView Cell Height

我有一组不同高度的堆栈视图: [Stack View A, Stack View B, Stack View C, Stack View D]

我想在 collectionView 中显示它们。每个 stackView 进入不同的 collectionView cell。我目前正在使用流布局根据视图在集合中的内容设置高度 viewController

是否可以对此进行抽象,以便集合 vc 不需要了解它们的任何信息?我可以传递要在单元格中显示的视图,并且单元格的高度由堆栈视图的高度决定。类似于固有内容大小,而不是在流布局委托 sizeforitem 方法中设置它。

您可以将 collectionView 的估计大小设置为 "Automatic" 以及单元格的大小,每个单元格将使用自动布局约束视图自行调整大小。

如 Xcode 11 发行说明中所述:

Cells in a UICollectionView can now self size with Auto Layout constrained views in the canvas. 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(). (45617083)