Set minimum content for UICollectionView

I want to set a minimum height for content size in a UICollectionView, so I can hide / show the UISearchbar, similar to the way it is done on iBooks.

However, I do not want to subclass the layout, because I want to keep the standard vertical layout for the UICollectionView.

any ideas?

+4
source share
3 answers

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 that you said you didn't want to subclass the layout. Anyway, I subclassed UICollectionViewFlowLayout and only modified the collectionViewContentSize method. He kept the standard vertical layout for me.

Edit: fooobar.com/questions/536327 / .... Here he says that UICollectionViewFlowLayout only supports one direction (vertical or horizontal), so should it be okay?

+8
source

you can try this quick solution, the search bar will be hidden if you have enough elements 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 that does the same thing is to use extra views. I just went. Subclass UICollectionReusableView, make sure you set the size of the header link in the stream layout

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

register an additional view with a 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, move the collection view at the beginning of the first element to hide the searchBar / header view.

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

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

0
source

This is a modified version of the answer to hansil. Holding only sizes 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; } 
0
source

All Articles