Search code examples
iosuitableviewuinavigationcontrolleruinavigationbarrubymotion

UISearchController search bar does not hide navigation bar


I have a UITableViewController as the root of my UINavigationController. Activating the search bar does not hide the navigation bar. I have tried so many different configurations and none of them seem to work.

class TableViewController < UITableViewController
  def viewDidLoad
    search_controller = UISearchController.alloc.initWithSearchResultsController(nil)
    search_controller.searchResultsUpdater = self
    search_controller.searchBar.delegate = self
    search_controller.searchBar.sizeToFit
    self.definesPresentationContext = true

    self.tableView.tableHeaderView = search_controller.searchBar
  end
end

Here's what my screen looks like when it's activated.

enter image description here

Why is this not working as intended?

Edit: the view is not dimming as well


Solution

  • I finally figured it out. The UISearchController needs to be assigned to a class variable so that the compiler does not dispose it.

    @search_controller = UISearchController.alloc
        .initWithSearchResultsController(nil)
    @search_controller.searchBar.sizeToFit
    @search_controller.searchBar.delegate = self
    
    table_header_view @search_controller.searchBar
    

    Hope this helps someone in the future so they don't have to waste hours wondering why it isn't working.