Search code examples
iosxcodeswiftsearchbarsearchdisplaycontroller

Searchbar disappears when typing


My application (iOS deployment target 8.2) shows a searchable table view. I use a Search Bar and Search Display Controller. The searching works (the table updates) but I have a very annoying visual bug that I can't seem to fix: As soon as the user starts typing a character in the searchbar, the bar disappears (but its still active).

I'm not allowed to post images yet, so here is a link to one hosted on postimage which hopefully explains my problem more clearly. From my understanding the search results are displayed in a separate tableview, and the searchbar becomes hidden behind the Navigationbar. I tried hiding the navigation bar but that didn't help.

Some code involving my SearchDisplayController:

func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
    self.filterContentForSearchText(searchString)
    return true
}

func filterContentForSearchText(searchText: String) {
     self.filDealer = self.dealers.filter({( deal: Dealer) -> Bool in
                let stringMatch = deal.name.lowercaseString.rangeOfString(searchText.lowercaseString)
                return (stringMatch != nil)
}

Solution

  • I found a solution myself, it's so simple that I feel rather stupid...

    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
        self.navigationController?.navigationBarHidden = true
    }
    
    func searchBarTextDidEndEditing(searchBar: UISearchBar) {
        self.navigationController?.navigationBarHidden = false
    }
    

    I don't know why it works but it does.