I have a view HeaderView
that is subclassing UICollectionReusableView
and which is a UIStackView
arranging some subviews with labels. The number and texts of those labels depend of the information requested to a service.
Such view is registered to a collectionView
this way:
collectionView.register(HeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HeaderView.reuseIdentifier)
Then I've implemented collectionView(_:viewForSupplementaryElementOfKind:at:)
method like this:
public func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
if let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
withReuseIdentifier: HeaderView.reuseIdentifier,
for: indexPath) as? HeaderView {
// Provide the headerView with the texts it needs to show
return headerView
}
fatalError("no view")
default:
fatalError("no view")
}
}
and also implemented collectionView(_:layout:referenceSizeForHeaderInSection:)
method.
My issue is that collectionView(_:layout:referenceSizeForHeaderInSection:)
is called first, and at that moment the headerView has not been instantiated, so I can't get its actual height. I can't provide a fix height either because, as I said, the height of such headerView will depend on the information I get from a service and it's going to be variable.
I'm not using either storyboard or xib files, I'm creating all the views and setting the layout in code.
I should also add that actually I need this HeaderView
to be the header of just one section in my collection view, so I may not need to dequeue and reuse it?