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