title: Create a custom UISearchDisplayController programmatically
body: <p>I was able to create an app using Storyboard - it has a navigation controller containing a table view controller with a search display controller and worked as expected.  </p>
  
  <p>I then decided to customize it by adding a <code>UIButtonBarSystemItemSearch</code> (magnifying glass) to the navigator bar so that when touched it would display the search bar over the navigator bar, just like <code>setDisplaysSearchBarInNavigationBar</code> does.  To do this I removed the <code>UISearchDisplayController</code> from the storyboard and added it programmatically as shown in the code below.  The <code>btnSearchClicked</code> function is called when the magnifying glass is touched, so the button is hooked up right.</p>
  
  <p>My project is very similar to the Apple example project "<a href="https://developer.apple.com/library/ios/samplecode/AdvancedTableSearch/Introduction/Intro.html" rel="nofollow">AdvancedTableSearch</a>" but without the scope bar.</p>
  
  <pre><code>- (IBAction) btnSearchClicked:(id)sender
  {
    //Set up search bar
    UISearchBar *mySearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
    [mySearchBar setDelegate:self];
    [mySearchBar setShowsCancelButton:YES animated:NO];
  
    // Set up search display controller
    UISearchDisplayController *mySearchController = [[UISearchDisplayController alloc] initWithSearchBar:mySearchBar contentsController:self];
    mySearchController.delegate = self;
    mySearchController.searchResultsDataSource = self;
    mySearchController.searchResultsDelegate = self;
    //mySearchController.displaysSearchBarInNavigationBar = YES;
    mySearchController.navigationItem.titleView.opaque = NO;
    [self.navigationController.navigationBar addSubview:self.searchDisplayController.searchBar];
  }
  </code></pre>
  
  <p>I am having two problems with this approach. <strong>The most important one is that even though I set the delegate properties, when I type into the search bar none of my delegate functions are called (specifically <code>shouldReloadTableForSearchString</code>)</strong>.</p>
  
  <p>The second problem is that the cancel button doesn't display despite calling the <code>setShowsCancelButton</code> method.</p>
  
  <p>I've been learning iOS for the last year and reading everything I can find on SO, but can't figure this one out.  This is my last resort and I will fall back to the old storyboard approach with standard search bar if I must.  The reason I like this other approach is that I have a very long list of contacts (~5000) and don't want to have to scroll to the top to get the search bar.</p>