0

How do I embed the search bar with the navigation bar items?

I used [searchBar sizeToFit] and it didn't work and without using displaysSearchBarInNavigationBar the navigation bar disappears when clicking on the searchBar.

I tried the answer in UISearchDisplayController UISearchDisplayController hiding navigation bar

-(void)viewDidLayoutSubviews{
[self.navigationController setNavigationBarHidden:NO animated:NO];
}

but it causes the UISearchDisplayController searchResultsTableView to be displayed below navigation bar while hiding the search bar.

Community
  • 1
  • 1
Olla Ashour
  • 301
  • 1
  • 11
  • I solved it using the answer [here](http://stackoverflow.com/a/24750397/2328392) i.e. using `[tableView setFrame:CGRectMake(0, 42, 320, 380)];` but the problem that now it is not compatible with other iOS devices and the cancel button does not work when there are data in search bar. Anyone else has any suggestions? – Olla Ashour Jan 07 '15 at 08:50
  • I tried another approach with `CGRectMake` `CGRect s = self.searchDisplayController.searchBar.frame; CGRect newFrame = CGRectMake(0, s.size.height, s.size.width, s.size.height*[filteredList count]); tableView.frame = newFrame;` but the cancel button issue remains and now the live search rows do not update even with `[tableview reload]`. – Olla Ashour Jan 07 '15 at 09:30

1 Answers1

0

I tried modifying the resultsTableView frame dimensions with below method while not having search bar in navigation bar i.e self.searchDisplayController.displaysSearchBarInNavigationBar = NO but this caused issues with cancel while in edit mode. This was taken from Moving SearchBar table view

- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView{

    CGRect s = self.searchDisplayController.searchBar.frame;
    CGRect newFrame = CGRectMake(0,
                                 s.size.height,
                                 s.size.width,
                                 s.size.height*[filteredList count]);

    tableView.frame = newFrame;


}

and then I found a better solution from this thread

and by just using [self.searchDisplayController.navigationItem setLeftBarButtonItem: self.sidebarButton] in viewDidLoad it solved my problem. What remains now is how to set searchResultsTableView frame sized based on content, but that's another issue. :)

Community
  • 1
  • 1
Olla Ashour
  • 301
  • 1
  • 11