Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use UISearchDisplayController with UICollectionViewController?

If you use a UISearchDisplayController with a UITableViewController, when the user taps the search bar it animates up to replace the nav bar.

I'd like to get that same effect when using a UISearchBar at the top of a UICollectionViewController. Any ideas?

like image 447
Hilton Campbell Avatar asked Sep 27 '12 17:09

Hilton Campbell


People also ask

What is a collection view?

A collection view manages an ordered set of content, such as the grid of photos in the Photos app, and presents it visually. Collection views are a collaboration between many different objects, including: Cells. A cell provides the visual representation for each piece of your content.

What is swift Uicollectionview?

An object that manages an ordered collection of data items and presents them using customizable layouts.


1 Answers

I had to add the searchBar programmatically as a subview of the UICollectionReusableView, I could never getting it working through IB as I kept getting prototype error when assigning an outlet. Adding the search bar in the implementation file worked for me.

The relevant methods are the following.

-(void)viewDidLoad
{
    [super viewDidLoad];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellIdentifier];
    _objectChanges = [NSMutableArray array];
    _sectionChanges = [NSMutableArray array];
    [self performFetch];
    searchBar = [[UISearchBar alloc]
                  initWithFrame:CGRectMake(0.0, 50.0, self.view.bounds.size.width,
                                           44.0)];
    searchBar.placeholder = @"Search for channels";
    searchBar.tintColor = [UIColor blackColor];
    searchBar.delegate = self;
}


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

    if ([kind isEqual:UICollectionElementKindSectionHeader])
    {
        header = [self.collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                    withReuseIdentifier:@"reuseHeader"
                                                           forIndexPath:indexPath];

        [header addSubview:searchBar];

    }
    return header;
}
like image 194
David Mast Avatar answered Oct 18 '22 00:10

David Mast