Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView can't collapse or remove supplementary view

I have a uicollectionview using flow layout that has a supplementary header view which is a view that I only sometimes want to display. So basically I want to have a button that will, when clicked, remove the supplementary view from the collection view and also re-place all the items in the collection view with the consideration that the header is gone. Is this possible? I've tried it repeatedly in many ways. Changing the reference header size, changing my answer to the delegate method for the header size, invalidating the layout, reloading the data etc etc etc. What am I missing?

I just ran a test. I think that it's related to using UIDynamics, what is it in UIDynamics that would override my delegate response for header section reference size?

like image 473
napierzaza Avatar asked Jun 16 '14 18:06

napierzaza


1 Answers

In your delegate's layout methods return the appropriate size for whatever state you're interested in:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (self.headerVisble) {
        return CGSizeMake(collectionView.bounds.size.width, 30.0f);
    }
    else {
        return CGSizeZero;
    }
}

Then when you need to update the layout call:

[collectionView.collectionViewLayout invalidateLayout];

The collection view will ask the Layout object for new info which will in turn ask your delegate. I believe it will animate the change too.

like image 120
kball Avatar answered Nov 07 '22 09:11

kball