How to make the collectionview cell to occupy full width

Hi,


I tried to strech the cell and give constraints to each and every label to occupy the full width still the cell does not occupy the full width any idea how to fix this


Thanks

Answered by aamirkhan in 422226022

I added this to the collectionviewcell


override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
       
        setNeedsLayout()
        layoutIfNeeded()
       
        let size = contentView.systemLayoutSizeFitting(layoutAttributes.size)
       
        var frame = layoutAttributes.frame
        frame.size.height = ceil(size.height)
        layoutAttributes.frame = frame
       
        return layoutAttributes
    }

The Cell is fitting perfectly now 😎

The cell width is computed automatically depending on spacing you have set.


The content inside has no impact (unless it is intended to force cell width ?).


Could you explain a bit more clearly:

- what you want

- what you get

- what you coded

Accepted Answer

I added this to the collectionviewcell


override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
       
        setNeedsLayout()
        layoutIfNeeded()
       
        let size = contentView.systemLayoutSizeFitting(layoutAttributes.size)
       
        var frame = layoutAttributes.frame
        frame.size.height = ceil(size.height)
        layoutAttributes.frame = frame
       
        return layoutAttributes
    }

The Cell is fitting perfectly now 😎

How to make the collectionview cell to occupy full width
 
 
Q