title: How can I use UISearchBar or UISearchDisplayController as a UICollectionView header?
body: <p>I have a UICollectionView which has nine cells in it. Each cell is a square, so that they're laid out in a 3 by 3 grid. This works nicely, and when the device rotates, I call <code>performBatchUpdates:completion:</code> to force the collectionView to layout the cells for the new orientation. </p>
  
  <p>Here's what it looks like without the search bar:</p>
  
  <p><img src="https://i.stack.imgur.com/97tji.png" alt="enter image description here"></p>
  
  <p>I'm now trying to add a UISearchBar on top of the collectionView, like so:</p>
  
  <p><img src="https://i.stack.imgur.com/BM0gG.png" alt="enter image description here"></p>
  
  <p>I've tried adding it as a header view, but instead of appearing on top, it consumes the entire screen, and the cells are pushed off to the right. To see them, you scroll over, where there appear as they do without the search bar. </p>
  
  <p>To add the search bar, I've tried two approaches. Neither works completely, and one seems like terrible practice. The first way I've tried is to add the search bar as a header view. My main view is a subclass of UICollectionViewController. Here's how I set things up:</p>
  
  <pre><code>- (void) viewDidLoad
  {
  
  // Set up some other things...
  
  //
  //  Register the header view class
  //  which installs a search bar inside 
  //  of itself.
  //
  
  [[self collectionView] registerClass:[PDReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
  
  }
  
  - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  {
      PDReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
      [header setFrame:[[[self navigationController] navigationBar] bounds]];
      return header;
  }
  
  - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  {
      if (section == 0) {
          return CGSizeMake(collectionView.frame.size.width, self.navigationController.navigationBar.frame.size.height);
      }
  
      return CGSizeZero;
  }
  </code></pre>
  
  <p>Inside of PDReusableView, I install a UISearchBar. This is what I get:</p>
  
  <p><img src="https://i.stack.imgur.com/TKgqB.png" alt="A search something"></p>
  
  <p>So this approach fails, because I can't wrangle a search controller or search bar into a reusable view. If I can't put it into a reusable view, it can't go into a UICollectionView.</p>
  
  <p>My other option is to resize the collection view, so it doesn't occupy the entirety of the screen. The question then becomes, where do I install the search bar in the view hierarchy? The simplest place, although likely incorrect, is the app delegate, where I set up the rest of the view hierarchy. I managed to get the search bar to appear above the collection view by installing it inside of <code>application:didFinishLaunchingWithOptions:</code> Here's my code for that:</p>
  
  <pre><code>UICollectionView *collectionView = [[self mainMenuViewController] collectionView];
  
  CGRect f = [collectionView frame];
  
  CGFloat height = [[[self navigationController] navigationBar] frame].size.height;
  
  f.size.height -= height;
  f.origin.y += height;
  
  [collectionView setFrame:f];
  
   // Cause the cells to be laid out per the new frame
  [collectionView performBatchUpdates:nil completion:nil];
  
  [[collectionView superview] addSubview:[self searchBar]];
  [[self searchBar] setFrame:[[[self navigationController] navigationBar] bounds]];
  </code></pre>
  
  <p>While I could proceed to add search logic to the app delegate, it would be inappropriate to do so, because I'd be maintaining a dataset, filtered search results, and a lot of other logic in the app delegate. I feel like the UICollectionViewController subclass is a much more appropriate place to put it. </p>
  
  <p>Is there a way to install the UISearchBar in a UICollectionView without using Interface Builder? How?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I'm using a vanilla UICollectionViewFlowLayout and using some of the delegate methods to set the size of cells etc.</p>