Search code examples
ios7uinavigationbaruisearchbaruisearchdisplaycontroller

Navigation bar items disappear when using UISearchDisplayController displaysSearchBarInNavigationBar


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.


Solution

  • 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. :)