6

In my app I use a UISearchController (new in iOS 8) to display search results. Is there a way to show a default set of results which appears when the user taps the searchBar without entering anything yet?
I found some questions and answers regarding this topic but they all used the old UISearchDisplayController and I couldn't seem to get the solutions working with the UISearchController.

Thanks

Logarythms
  • 71
  • 3

2 Answers2

0
self.searchController.searchBar.text = @"\n";

Seems to do the trick. Hacky - but hey, seems like we don't have a whole lot else to work with.

NSTJ
  • 3,858
  • 2
  • 27
  • 34
0

I think this method is better, be careful when searchBar is empty then preload tableview will disappear again.

UISearchBarDelegate

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    if searchText.isEmpty {
        dispatch_async(dispatch_get_main_queue()) {
            self.searchController.searchResultsController?.view.hidden = false
        }
    }
}

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    dispatch_async(dispatch_get_main_queue()) {
        self.searchController.searchResultsController?.view.hidden = false
    }
}

UISearchControllerDelegate

func willPresentSearchController(searchController: UISearchController) {
    dispatch_async(dispatch_get_main_queue()) {
        self.searchController.searchResultsController?.view.hidden = false
    }
}
Zhiping Yang
  • 379
  • 3
  • 7