It is not visible because the header is not drawn.
You need to create a few elements:
You have declared a header section in the collectionView Attributes Inspector by enabling Section Header in Accessories:
create a class for the headerView (new file); I named it Item1HeaderView but you can choose something else of course:
import UIKit
class Item1HeaderView: UICollectionReusableView {
@IBOutlet weak var titleLabel: UILabel!
@IBAction func headerButtonAction(_ sender: UIButton) {
print("button was hit")
}
}
That's Swift but is is immediate to write this in objC.
set the header view to the right class (Item1HeaderView) in its Identity Inspector
set the proper identifier in its Attributes Inspector:
Create button and label inside ;
Connect button and Label to their IBAction and IBOutlet in Item1HeaderView class.
In the class where the CollectionView is, add this delegate func:
func collectionView(_ collectionView: UICollectionView,
viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionView.elementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
withReuseIdentifier: "\(Item1HeaderView.self)",
for: indexPath) // Or simply withReuseIdentifier: "Item1HeaderView"
headerView.backgroundColor = .yellow
guard let typedHeaderView = headerView as? Item1HeaderView else { return headerView }
typedHeaderView.titleLabel.text = "Hello"
return typedHeaderView
default:
print("No header")
}
return UICollectionReusableView()
}
That should now work.
See full details in this tutorial:
h t t p s : / / w w w.raywenderlich.com/21959913-uicollectionview-tutorial-headers-selection-and-reordering