How can I make a UISearchController start hidden?

2.4k views Asked by At

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

3

There are 3 answers

1
aksh1t On BEST ANSWER

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

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

From iOS 11.0 onwards you can use,

self.navigationItem.searchController = self.searchController;

The search bar will be hidden, unless you swipe down to reveal it.

0
Pavle Mijatovic On

Here is for swift 4

tableView.setContentOffset(CGPoint(x: 0, y: searchController.searchBar.frame.size.height), animated: true)