7

I am using a UISearchController inside a Container view in a UITableView. I am adding the search bar like this:

self.resultsTableController = [self.storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([ResultViewController class])];

self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
self.searchController.searchResultsUpdater = self;
[self.searchController.searchBar sizeToFit];

self.searchController.hidesNavigationBarDuringPresentation = NO;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.resultsTableController.tableView.delegate = self;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = YES;
self.searchController.searchBar.delegate = self;
self.definesPresentationContext = NO;

See the following screenshots. It hides all the search results that are above the search bar:

original image when searchbar is not active. SearchBar active and all above elements get hidden.

Please help me out why i am getting this?

Undo
  • 25,204
  • 37
  • 102
  • 124
Umair Suraj
  • 478
  • 9
  • 21

1 Answers1

1

I found searchcontroller is little tricky. I had lot of issues and I found that specific order matters. The following combination is what worked for me

        searchController.searchResultsUpdater = self
        searchController.searchBar.delegate = self
        searchController.dimsBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = "search"
        searchController.searchBar.returnKeyType = .done
        searchController.searchBar.searchBarStyle = .minimal
        searchController.hidesNavigationBarDuringPresentation = false
        searchController.searchBar.sizeToFit()
        definesPresentationContext = true
        if(tableView.tableHeaderView == nil){
            tableView.tableHeaderView = searchController.searchBar
        }

You can have

searchController.hidesNavigationBarDuringPresentation = false