How to adjust height of UICollectionView to be the height of the content size of the UICollectionView?

后端 未结 14 1157
死守一世寂寞
死守一世寂寞 2020-11-28 03:51

I would like the UICollectionView (The red one) to shrink to the height of the content size in this case UICollectionViewCells(the yellow ones) because there is a lot of emp

14条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 04:29

    Get the height of the cell. Something like this

    let cellHeight = cell.frame.height
    

    Get the origin of the collection view

    let cvOrigin = collectionView.frame.origin
    

    Get the width of the collection view

    let cvWidth = collectionView.bounds.width
    

    Set the frame of the content view

    collection.frame = CGRect(x: cvOrigin.x, y: cvOrigin.y, width: cvWidth, height: cellHeight )
    

提交回复
热议问题