Struggling to make collection view height fit content

Viewed 47

I have a bit of a special scenario and no matter what StackOverflow answer I find, it doesn't seem to work for me. I have a collection view with a dynamic number of cells (anywhere between 2-3 cells). However, the height of this collection view will always be the same, no matter how many cells there are.

Here is the logic I have for setting the cell heights:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    // The cell's width and height, to be calculated
    var cellWidth: CGFloat = 0.0, cellHeight: CGFloat = 0.0
    
    // The number of cells to show, 2-3
    let count = numberOfCellsToShow
    
    // The width of the collection view
    let collectionViewWidth = collectionView.bounds.size.width
    
    // Calculate the maximum height for collection view (16:9 ratio)
    let collectionViewMaxHeight = collectionViewWidth / (16 / 9)
    
    // Get the width and height of each cell, with 4dp of spacing between cells
    if (count == 2) {
        cellWidth = (collectionViewWidth - CGFloat(4)) / 2
        cellHeight = collectionViewMaxHeight
    } else if (count == 3) {
        cellWidth = (collectionViewWidth - (CGFloat(4) * 2)) / 3
        cellHeight = collectionViewMaxHeight
    }
    
    return CGSize(width: cellWidth, height: cellHeight)
}

Now, I want to make sure that the height of the collection view fits the content, but this is where I'm struggling to make it happen.

My collection view is inside a stack view, with all constraints set to the edges of the stack view.

What do I need to do to set the height of my collection view to fit the contents?

0 Answers
Related