change the height of a UICollectionReuseableView (collection section header) dynamically
Asked Answered
C

4

31

I am trying to set the height of the section headers for a UICollectionView dynamically, but using the code below, I am not seeing anything change. The view is drawn with the correct items in it but the height will not budge. Sorry if this is a repeat question, but I can't seem to find anything related specifically to the UICollectionView object. Thanks in advance.

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath
{
    PhotoVideoHeaderCell *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                          withReuseIdentifier:@"videoHeaderView"
                                                                                 forIndexPath:indexPath];
    if (indexPath.section == 0) {
        // photos
        [headerView setSection:@"Photo"];
    } else {
        [headerView.VehicleDetailView removeFromSuperview];
        CGRect frame = headerView.frame;
        frame.size.height = 60;
        [headerView setFrame:frame];
        [headerView setNeedsDisplay];
        [headerView setBackgroundColor:[UIColor grayColor]];
        [headerView setSection:@"Video"];
    }

    return headerView;
}
Corena answered 17/1, 2013 at 16:48 Comment(0)
M
77

Your delegate should implement the following function, assuming you're using a flow layout:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;

You can return a different size for each header. In horizontally scrolling collection views, only the width is used. In vertically scrolling ones, only the height is used. The unused value is ignored — your view will always be stretched to fill the full height/width of horizontal/vertical collection views, respectively.

There is a corresponding method for footers, too.

Medellin answered 17/1, 2013 at 17:27 Comment(5)
this works, but how do i tell which section i am editing, i only want to do it for section at index 1Corena
Use the section parameter in the method. Store which section you're editing in an ivar and then compare them. If they're not the same, return the layout's headerReferenceSize.Medellin
sorry for being a complete noob here but the headerReferenceSize is not part of the collectionViewLayout... do i need to create an instance of the header to read the frame?Corena
Nope - cast the collectionViewLayoutreturn [(UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout headerReferenceSize];.Medellin
Hero again @AshFurrow - that is exactly what I was after, perfect :) Thank you!Overcasting
T
6

Accepted answer in Swift 3 and 4:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width: collectionView.frame.size.width, height: 250)
}
Toh answered 22/11, 2017 at 20:28 Comment(0)
A
5

Well, instead of using the noisy delegate method, I prefer using this: (only works if you have a unique header size)

    let layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout // Assuming you use the default flow layout 
    layout.headerReferenceSize = CGSize(width: 42, height: 42) //Set the header size

Works also with the itemSize:

    layout.itemSize = CGSize(width: 42, height: 42) //Set a custom item size

This works great for setting an item size relative to your screen width for exemple.

Anstus answered 5/4, 2018 at 7:51 Comment(0)
B
1

Try something like this:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *header = [siteMapCollection dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: @"headerID" forIndexPath: indexPath];
    NSString *headerText = @"This is my header";
    UIFont *labFont = [UIFont fontWithName: @"HelveticaNeue-CondensedBold" size: 20.0];
    CGSize textSize = [dummyText sizeWithFont: labFont];
    UILabel *headerLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, header.frame.size.height - (textSize.height + 12), header.frame.size.width, textSize.height + 8)];
    [headerLabel setFont: labFont];

    [header addSubview: headerLabel];

    return header;
}
Bijou answered 17/1, 2013 at 17:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.