Set minimum contentsize for UICollectionView

2019-02-18 08:36发布

问题:

I want to be able to set the minimum height of the content size within a UICollectionView, so I can hide/show a UISearchbar, similar to the way it's done on iBooks.

However, I don't want to subclass the layout as I want to keep the standard vertical layout for a UICollectionView.

any ideas?

回答1:

You can do this by subclassing UICollectionViewFlowLayout and overriding the method

-(CGSize)collectionViewContentSize
{ //Get the collectionViewContentSize
     CGSize size = [super collectionViewContentSize];
     if (size < minimumSize) return minimumSize;
     else return size;
}

Edit: I just realized you said you didn't want to subclass the layout. Anyway, I subclassed UICollectionViewFlowLayout and only modified the collectionViewContentSize method. It kept the standard vertical layout for me.

Edit: https://stackoverflow.com/a/14465485/2017159. Here it says UICollectionViewFlowLayout only supports one direction (vertical or horizontal), so it should be fine?



回答2:

you can try this quick solution, the search bar will be hidden if you have enough items to fill the screen. Of course you can change the UISearchBar below with any custom view.

collectionView.contentInset = UIEdgeInsetsMake(44.0, 0.0, 0.0, 0);
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, -44, collectionView.frame.size.width, 44)];
[collectionView addSubview:searchBar];
if([items count] != 0){
    [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:NO];
}

another solution which does exactly the same thing is to use supplementary views. I just had a go. Create a subclass of UICollectionReusableView, make sure you set the header reference size on the flow layout

[flowLayout setHeaderReferenceSize:CGSizeMake(0, 44.0)];  

register the supplementary view with collection view

[playersCollectionView registerClass:[MySupplementaryView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeader"];

and implement the UICollectioViewDataSource method

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

    if ([kind isEqual:UICollectionElementKindSectionHeader]){
        header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"MyHeader" forIndexPath:indexPath];
        header.headerLabel.text = @"bla bla";
    }
    return header;
}

And finally after each reload, reposition the collection view at the start of the first item to hide the searchBar/header view.

if([items count] != 0){
    [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:NO];
}

Tutorials for supplementary views techotopia.com, appcode.com, mobinius



回答3:

This is a tweaked version of khangsile's answer. Capping only the dimentions that are actually smaller than the minimum size

- (CGSize)collectionViewContentSize
{
    CGSize size = [super collectionViewContentSize];

    size.width  = MAX(size.width, self.minimumContentSize.width);
    size.height = MAX(size.height, self.minimumContentSize.height);

    return size;
}