Search code examples
ios8xcode7ios9uisearchcontroller

Section header on top of UISearchBar on iOS 8. Works on iOS 9


Using UISearchController to add filtering to a UITableView. The section header overlaps with the search bar on iOS 8 using Xcode 7. This looks good in iOS 9. What workaround can I get in place so iOS 8 looks like the iOS 9 version?

override func viewDidLoad() {
    super.viewDidLoad()
    self.searchController.searchResultsUpdater = self
    self.searchController.hidesNavigationBarDuringPresentation = false
    self.searchController.dimsBackgroundDuringPresentation = false
    self.tableView.tableHeaderView = searchController.searchBar
    self.definesPresentationContext = true
    self.searchController.searchBar.sizeToFit()

    tableView.sectionIndexBackgroundColor = UIColor.clearColor()
}

UITableView with sections and index on iOS 8 Simulator


Solution

  • Made the change to be:

        self.searchController.searchResultsUpdater = self
        self.searchController.hidesNavigationBarDuringPresentation = false
        self.searchController.dimsBackgroundDuringPresentation = false
        self.searchController.definesPresentationContext = true
        self.searchController.searchBar.sizeToFit()
        self.navigationItem.titleView = self.searchController.searchBar;
        tableView.sectionIndexBackgroundColor = UIColor.clearColor()
    

    So setting the definesPresentationContext on the searchController, not the view controller.