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