0

I have a UICollectionView (using flow layout) with cells using auto layout. To enable this I have set the estimatedItemSize to non zero values.

To trigger cellForRowAt I need to set a non zero height for the collection view. This can either be an explicit height constraint or an inherited height from the parent view.

However this seems to lead to a scenario where if the dynamic cell height calculated by auto layout is greater than the height of the collection view the error;

"... the item height must be less than the height of the UICollectionView..."

understandably occurs. What is the preferred solution to this? It seems like a catch 22 where I have to set a height of some value for cellForRowAt to be called but this value can potentially cause the above error.

The desired behaviour I'm attempting for is the collection view height to match the dynamic height of the cells (this is a horizontally scrolling collection view where each cell is "full screen").

UPDATE:

To clarify further here is are the steps are followed;

On view load, set the collection view height to a minimum, say 200. A non zero value is required here to trigger the data load.

Load the collection view data where cellForRowAt will initiate the creation of the cells. The cells will be sized using Autolayout.

If the sized cell height < 200, all is well.

If the sized cell height > 200, the flow layout error is logged as expected.

To resolve this, the cell needs to notify it's height in layoutSubviews. The collection view height is then adjusted to the new height if it is greater.

The net effect is I have a expanding collection view with a minimum height (of 200) and grows to the max height of the largest cell.

Chamitha
  • 215
  • 3
  • 11
  • "The desired behaviour I'm attempting for is the collection view height to match the dynamic height of the cells" Why not just work out the height the cells need to be, and set both the collection view and the cells to that height? – matt Apr 26 '18 at 03:31
  • 4
    How about this solutions ? [UICollectionView - dynamic cell height?](https://stackoverflow.com/questions/28161839/uicollectionview-dynamic-cell-height) – AechoLiu Apr 26 '18 at 03:36
  • @matt I was hoping that is what auto layout is supposed to do? It calculates the cell height correctly. Is there no way to base the collection view height on this or set it to be equal to the auto calculated cell height somewhere? – Chamitha Apr 26 '18 at 03:37
  • @AechoLiu the complication with my cell is that the controls in the cell are dynamically created when configured. Therefore I cannot using a sizing cell. – Chamitha Apr 26 '18 at 03:40
  • I guess my dilemma is how to set the collection view height rather than calculating the cell height. I.e. the initial height I set to allow cellForRowAt conflicts with the calculated cell height. – Chamitha Apr 26 '18 at 04:01
  • I still don’t see the problem. You can use autolayout internally, ask the cell what height is dictated by those internal constraints, and set the collection view height accordingly. – matt Apr 26 '18 at 04:08

0 Answers0