Search code examples
iosswiftuitableviewuisearchbaruisearchcontroller

Blank space between SearchBar and NavigationBar - Swift


I have a simple tableViewController, a searchBar with scopes and a navigationBar.

When I click on my searchBar I see my scopes and all works. Then I click on a row and I go to my DetailPage and the search bar isn't hidden (I don't know why).

So I click cancel and then I return to my tableView. When I return to my tableView there is a blank space between my SearchBar and my NavigationBar.

Blank space

This is my code:

      self.resultSearchController = ({
            let controller = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self
            controller.hidesNavigationBarDuringPresentation = true
            controller.dimsBackgroundDuringPresentation = false
            controller.searchBar.sizeToFit()
            controller.searchBar.scopeButtonTitles = ["Title", "Author", "Location", "Price", "User"]
            self.tableView.tableHeaderView = controller.searchBar
            return controller
        })()

 func updateSearchResults(for searchController: UISearchController)
    {
        let scopes = resultSearchController.searchBar.scopeButtonTitles
        let currentScope = scopes![resultSearchController.searchBar.selectedScopeButtonIndex] as String
        filterContentForSearchText(searchText: searchController.searchBar.text!, scope: currentScope)
    }

Solution

  • try to Add

      self.automaticallyAdjustsScrollViewInsets = false;
    
      self.extendedLayoutIncludesOpaqueBars = true
    

    in viewDidload.