Nicolas Miari
Nicolas Miari

Reputation: 16256

UISearchBar disappears on cancel

My app is iPad landscape, using autolayout.

The initial view has a table view that occupies only half of the screen (the other half is a MapKit view).

This view controller containing that table (and the map view) is the top view controller of the app's root navigation controller.

On view load, I instantiate the UISearchController and add its search bar to the table view's header view, as is standard procedure.

If I tap the search bar and enter some text, results are displayed. If I cancel the search here, no problem.

If instead, I select one of the result rows, and push into the next screen, and pop back, the search bar is gone. I can restore it by calling again:

self.tableView.headerView = self.searchController.searchBar

in viewDidAppear(), but that only works until I tap the search bar's 'cancel' button (at which point, it disappears again - right after the scope button collapse animation ends).

I have seen very many similar questions, but none describes my exact symptoms, and none of the solutions (or anything else I could think of):

// In viewDidLoad():

self.definesPresentationContext = true

self.searchController.definesPresentationContext = true

self.extendedLayoutIncludesOpaqueBars = true

self.navigationController?.extendedLayoutIncludesOpaqueBars = true

self.navigationController?.navigationBar.translucent = true

...seem to work in my case.

The weirdest part is, I have other similar table views with search interfaces (presented in modal view controllers), with no difference I can spot with respect to configuration, and those work alright...

I know I must be missing something...

Upvotes: 1

Views: 2018

Answers (2)

David.Chu.ca
David.Chu.ca

Reputation: 38704

I had similar issue. Sometimes, when Cancel button is tapped, the search bar is disappeared too. What I found is that if the search bar is reset to table header again, this could cause search bar disappeared. Therefore I have the following updated codes to resolve the issue.

func setupSearchController(_ ctr: UISearchController?)  {
  if let sc = car { // search bar is not nil
    if #available(iOS 11.0, *) {
      navigationItem.searchController = sc
    } else {
      // Fallback on earlier versions
      if tableView.tableHeaderView == nil {
        tableView.tableHeaderView = sc.searchBar
      }
    }
  } else {
    if #available(iOS 11.0, *) {
      navigationItem.searchController = nil
    } else {
      // Fallback on earlier versions
      tableView.tableHeaderView = nil
    }
  }
}

Note: for iOS 11 or above, the search bar is set to navigation item.

Upvotes: 1

Nicolas Miari
Nicolas Miari

Reputation: 16256

After several hours of trying everything, I found the offending code:

func searchBarTextDidEndEditing(searchBar: UISearchBar)
{
    // Dismiss the keyboard
    self.resultSearchController.resignFirstResponder()

    // Reload of table data
    self.resultSearchController.loadView()
}

After commenting these two lines out -or better, the whole method (since I'm not doing anything else in it)- the problem sorted itself out.

NOTE: One of my coworkers implemented that method while trying different things to get the search to work as expected, and it somehow stuck...

Upvotes: 0

Related Questions