I have a horizontal UICollectionView
that's contained inside a UICollectionView
header of a vertical UICollectionView
. The header of the vertical UICollectionView
has a dynamic height that changes due to user interaction (dragging the outer UICollectionView
down expands it, scrolling up collapses it). The horizontal UICollectionView
is constrained to the size of the header and will also shrink in height when the header is reduced in height. When this happens I get this error
the behavior of the UICollectionViewFlowLayout is not defined because: 2018-07-19 13:42:09.959 IDAGIO[81891:2239798] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values. 2018-07-19 13:42:09.959 IDAGIO[81891:2239798] Please check the values return by the delegate.
because the UICollectionView
shrinks in height, but the cells of it keep their old size and are therefore higher than allowed (I only get this error while scrolling up, so it's not related to any insets, the heights are fine, they just don't match exactly at that point in time).
I already tried to call collectionView.collectionViewLayout.invalidateLayout()
every time I get a scroll event of the vertical collection view and then return the collection view size infunc collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
but the layout update then is always a bit too late, so I still get the above error. Also it looks weird as the cell height also visually is a bit behind.
My question: is there a way to dynamically respond to height changes of a horizontal UICollectionView
so the cell height is updated accordingly and in time? (Maybe setup some height constraint that automatically keeps the cell height equal to the collection view height?)
I know about dynamic cell sizing, but that usually means to constrain a cell to the size of it's content. What I want to achieve is to always constrain the height of the cell (and it's content) to the collection view height.
sizeForItemAtIndexPath
returnCGSizeMake(customWidth, collectionView.height);
and reload your collection each time it gets new height. – TheTiger