I know there are already a lot of posts around this topic, but they somehow didn't lead me to a solution and at this point, after trying for days I don't know what to do.

So I have a UICollectionView where I have a header. For the header I created my own UICollectionReusableView . It contains a StackView with two labels in it. Both of them have dynamic sizes (Lines = 0). These are the the constraints for the StackView (I also tried setting the bottom constraint to = 0):

StackView constraints

I calculate the header size like this...

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { let item = displayItems.object(at: UInt(section)) as! MySectionDisplayItem let reusableView: MyReusableView = UIView.fromNib() reusableView.setTitle(text: item.getTitle()) .setSubtitle(subtitle: item.getSubtitle()) return reusableView.systemLayoutSizeFitting(UILayoutFittingCompressedSize) }

...and return the view to be displayed like this:

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { let item = displayItems.object(at: UInt(indexPath.section)) as! MySectionDisplayItem let reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "myReusableView", for: indexPath) as! MyReusableView reusableView.setTitle(text: item.getTitle()) .setSubtitle(subtitle: item.getSubtitle()) return reusableView }

In my UICollectionReusableView and UICollectionView Auto Layout is enabled.

Some things I tried:

set preferredMaxLayoutWidth to different positive values on my labels

to different positive values on my labels embed the labels in separate views

work with constraints instead of using a StackView

I hope I didn't mix up some of the solutions I found, but anyways I don't think such a "simple" thing can be that hard to realize.