Create a UICollectionReusableView header class

Screen Shot 2017-01-09 at 2.55.33 PM.png

Set this header as the designated class for your Reusable view element in your storyboard along with it’s id

Screen Shot 2017-01-09 at 2.56.44 PM.png

Screen Shot 2017-01-09 at 3.11.25 PM.png

and next tab

Screen Shot 2017-01-09 at 3.11.38 PM.png

Now drag that label into the Header you just created as a outlet

Screen Shot 2017-01-09 at 3.00.43 PM.png

Now use it, and set the title label in your ViewController that already implements UICollectionDataSource

Screen Shot 2017-01-09 at 3.13.05 PM.png

You should then have a beautiful header like this

Screen Shot 2017-01-09 at 3.14.38 PM.png

Code

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    SPTMessageTrackHeaderView *header = nil;

    if (kind == UICollectionElementKindSectionHeader) {
        header = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                    withReuseIdentifier:@"SPTMessageTrackHeaderView"
                                                           forIndexPath:indexPath];
        header.titleLabel.text = @"Popular Tracks";
    }

    return header;
}

 

 

Advertisements