简体   繁体   中英

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]

I want to display them in a collectionView . Every stackView goes into a different collectionView cell . I am currently using a flow layout to set the height based on what the view is in the collection viewController .

Would it be possible to abstract this so that the collection vc doesn't need to know anything about them? 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.

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.

As stated in the Xcode 11 Release Notes:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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