0

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.

ketaki Damale
  • 559
  • 6
  • 21
Bilbo Baggins
  • 3,494
  • 6
  • 36
  • 60
  • You can try -> https://stackoverflow.com/questions/44187881/uicollectionview-full-width-cells-allow-autolayout-dynamic-height – dahiya_boy Nov 29 '19 at 11:47

1 Answers1

1

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)

Andy
  • 432
  • 2
  • 7
  • If I do this, do I need to use the collection view flow layout delegate? – Bilbo Baggins Nov 30 '19 at 10:28
  • 1
    No. Indeed, I got curious about it and have created a sample code that implements dynamic cell's height without extending flow layout delegate. You can check it on https://github.com/valengo/dynamicCollectionViewCell/blob/master/AutoSizedCellCollection/ViewController.swift I guess you can have fun improving it! – Andy Dec 01 '19 at 22:43
  • Thanks Andy, I'll check it out – Bilbo Baggins Dec 02 '19 at 21:51