3

I am trying to add a search bar as the header to a custom flow layout class.

I know how to add it in storyboard if the flow layout is not custom by simply checking the section header box, but as soon as i use a custom class, that option is no longer available.

Mostly I am having trouble conceptualizing layout attributes and how to implement them. Also I'm not sure if I should be putting methods in my datasource, view controller, or flow layout subclass, so please be specific about those.

The tutorial on Ray Wenderlich only shows the storyboard method.

fragle
  • 51
  • 6
  • What do you mean by `UISearchBar` as a header to a custom flow layout? Flow layout is used to adjust and modify flow of the `UICollectionViewCell` within `UICollectionView`. Also, are you trying to add a `UISearchBar` to each `UICollectionViewCell`? Also most likely what you are trying to do is best done programatically. – july Mar 02 '14 at 01:27
  • Im trying to add a single search bar to the top of a collection view. The reason I want to add it as a header is so that it scrolls up with the collection view, instead of remaining static while the collection view scrolls under it. I know this is a fairly complex question and there might be a better approach. Let me know if you think of one – fragle Mar 02 '14 at 06:04
  • that's more of a pain with storyboards, but easy to do programmatically. i'll explain in the answer section – july Mar 02 '14 at 20:54
  • Do u want the that header view to be static or it should be visible only if the collection view is scrolled to top? – Sanjeeva kumar kalva Oct 20 '14 at 08:34
  • Hi @fragle **want to add it as a header is so that it scrolls up with the collection view, instead of remaining static while the collection view scrolls under it** - sure, it's a common problem. I provided the total solution below, including all code! :) Did you try it? – Fattie Oct 21 '14 at 13:26

1 Answers1

0

Create HeadeView using UICollectionReusableView and call the following delegates.

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
    {
        if (kind == UICollectionElementKindSectionHeader) {
            HeaderView *headerView = nil;
        headerView = (ProductListingHeaderView*)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

        if (headerView == nil) {
            headerView=[[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil] objectAtIndex:0];
        }
    return headerView;

        }
        return nil;
    }

    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
        if (![collectionView numberOfItemsInSection:section]) {
            return CGSizeZero;
        }
        return CGSizeMake([(UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout headerReferenceSize].width, searchBarHeight);

    }