Adding UILabel forces UICollectionViewCell to shrink width

Viewed 399

I have a UICollectionView with cells that are different widths.

Each cell has a footer view that contains a UILabel. I'd like that label to be full width, regardless of how long the text is, I'd like the label to always span the width of the footer view within the UICollectionViewCell.

To attempt this I have anchors my UILabel as follows -

enter image description here

To the top of the view To the trailing of it's neighbour To the trailing of the view

The effect of the however is that when my cell is rendered, it is no longer the width set in sizeForItemAt and instead is shrunken to the trailing edge of the icon.

I cannot understand why, I am new to using a xib and historically would build my views programmatically, where I have not encountered this issue.

My sizeForItem is -

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return .init(width: collectionView.bounds.width / 2, height: 480)
    }
2 Answers

You need to change horizontal contentHuggingpriority of the label to low most value so it can stretch freely

Perhaps you could try using the center Y and center X constraint and switch the leading and trailing constraints of the label to >=? This should stop the label from disrupting the content size.

Related