Search code examples
iphoneiosuisearchbaruisearchdisplaycontroller

UISearchDisplayController frame for UITableView in iOS


  • I have a UITable that has a UISearchDisplayController.
  • The UITable is less than the width of the screen (it's 280px width centered).
  • When I tap on the search bar, the UISearchDisplayController table is all the way to the left of the screen.
  • Even when changing the frame of the table of the UISearchDisplayController, I still get the same positioning.

I set the frame here:

- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView  {
    tableView.backgroundColor = [UIColor colorWithRed:29.0/255.0 green:30.0/255.0 blue:32.0/255.0 alpha:1];
    tableView.rowHeight = self.myTable.rowHeight;
    tableView.frame = myTable.frame;
    NSLog(@"search table origin: %f",tableView.frame.origin.x);
}

Even weirder, when I log the search table position at the end, it shows 16. However, it is at position 0 in the view.

Any help is appreciated.


Solution

  • I answered my own question. The frame needs to be set in this delegate method:

    - (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView  {
    
        tableView.frame = self.myTable.frame;
    
    }