title: Programmatically activating UISearchBar blocks user interactions with it
body: <p>I have a simple table view inside a navigation controller with the toolbar visible. On the toolbar is a search button, and the table view's table header view is set to my <code>UISearchBar</code>. If I scroll the table view up and then tap on the search bar to activate it, everything happens normally. But if I tap on the search button, which activates search bar programmatically, then all interactions with the search bar are disabled (looks like it's blocked by something), and the user can't clear the search, cancel the search or even navigate around the text in the textfield.</p>
  
  <pre><code>-(void)viewDidLoad
  {
      [self setupSearchBar];
  }
  
  -(void)setupSearchBar
  {
      self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44.0)];
      self.searchBar.autocorrectionType = UITextAutocorrectionTypeDefault;
      self.searchBar.delegate = self;
      self.tableView.tableHeaderView = self.searchBar;
  
      self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar
                                                            contentsController:self];
      self.searchController.delegate = self;
      self.searchController.searchResultsDataSource = self;
      self.searchController.searchResultsDelegate = self;
  
      [self.searchController.searchResultsTableView registerNib:[UINib nibWithNibName:@"ItemCell" bundle:nil]
                                     forCellReuseIdentifier:CellIdentifier];
  
      for (UIView* subview in self.searchBar.subviews) {
          if ([subview conformsToProtocol:@protocol(UITextInputTraits)]) {
              [(UITextField*)subview setKeyboardAppearance:UIKeyboardAppearanceAlert];
              [(UITextField*)subview setReturnKeyType:UIReturnKeyDone];
          }
      }
  
      self.tableView.contentOffset = CGPointMake(0, self.searchBar.frame.size.height);
  }
  
  -(IBAction)search:(id)sender
  {
      [self.searchBar becomeFirstResponder]; // also tried [self.searchController setActive:YES animated:YES];
  } 
  </code></pre>
  
  <p>I've set up my search display controller with an <code>NSFetchedResultsController</code> like described in <a href="https://stackoverflow.com/a/4481896/1827743" title="How to filter NSFetchedResultsController (CoreData) with UISearchDisplayController/UISearchBar">this SO post.</a></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I worked around this problem by making my <code>UITableView</code> scroll to the top before setting the <code>UISearchBar</code> to first responder. Why does this work? I don't know, seeing that the becoming first responder part is not an issue, but if the search bar is not on the screen when it becomes first responder, the cancel and clear buttons won't work. This is my code now.</p>
  
  <pre><code>-(void)search:(id)sender
  {
      [self.tableView setContentOffset:CGPointMake(0, -self.tableView.contentInset.top) animated:NO];
      double delayInSeconds = 0.1;
      dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
      dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
          [self.searchController setActive:YES animated:YES];
          [self.searchController.searchBar becomeFirstResponder];
      });
  }
  </code></pre>
  
  <p>If someone has any insights as to why this works, I'm all ears.</p>