Search code examples
iosobjective-cuisearchbaruisearchcontroller

How can I make a UISearchController start hidden?


I have added a UISearchController to my code using the following method:

self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.scopeButtonTitles = @[];
self.searchController.searchBar.delegate = self;

self.tableView.tableHeaderView = self.searchController.searchBar;
[self.searchController.searchBar sizeToFit];
self.definesPresentationContext = YES;

This creates my search controller and adds it to the top of my tableView. Annoyingly it starts visible though:

enter image description here

I can hide it by sliding it up under the navigation bar which suggests the underlying functionality of the code is working but I can't get it to start hidden so I can slide it down.

I have tried adjusting the edge insets, I have tried setting the navigation bar to translucent, I have tried to go through the search bar tutorials online but nothing seems to be dealing with this issue.

Any help very welcome


Solution

  • Did you try setting the content offset of your table view?

    [self.tableView setContentOffset:CGPointMake(0, self.searchController.searchBar.frame.size.height) animated:NO];