Search code examples
iosswiftuicollectionviewuicollectionviewlayoutuicollectionreusableview

how can I display image and label in Section Header of my UICollectionView?


In my swift app I'm using UICollectionViewController in which I'm displaying photos one next to each other:

enter image description here

I also added on top of the collection view a header, I did it through story board by checking this option:

enter image description here

So now I see that header in my story board, I can even put there some labels:

enter image description here

but how can I display there anything? I tried attaching IBOutlet from this label to the class that is a main class of that view, but that brings me error saying that outlets from Collection reusable view cannot be attached directly to UICollectionViewcontroller. What can I do in this situation?


Solution

  • To accomplish this you need to subclass UICollectionViewController and assign this class as the of your Header and Footer. Then in that class you can create the Outlet connection of the views you assign in the storyboard.

    Subclass the UICollectionReusableView:

    enter image description here

    Assign your subclass to the UICollectionViewController header:

    Assign subclass to the UICollectionViewController header

    Drag elements to the header in the storyboard then create the IBOutlet connection.

    Create the IBOutlet connection