Programmatically create UICollectionView with custom headers
So I figured it out, with inspiration from Mohamad Farhand.
The problem was that I had to register the subclass itself with the collectionView, instead of UICollectionReusableView.self
, I used the instance of the subclass someView
.. So this solved my problem:
collectionView.registerClass(SupView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader , withReuseIdentifier: "someRandonIdentifierString")
And how to initialize the view:
someView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "someRandonIdentifierString", forIndexPath: indexPath) as! SupView
Note that Swift 4.1 renames the ofKind: constant as UICollectionView.elementKindSectionHeader
.