2

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

Jason Hocker
  • 6,879
  • 9
  • 46
  • 79

2 Answers2

1
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44)];
[self.tableView.tableHeaderView addSubview:self.searchController.searchBar];

Create a new UIView and add searchBar as a subview, then set the tableView's header view to the new created view will solve the issue.

patrick kuang
  • 231
  • 3
  • 5
-1

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.

Jason Hocker
  • 6,879
  • 9
  • 46
  • 79
  • How putting the seachBar in titleView instead of tableHeaderView solved the problem here? It's not really solving the issue... – Mikael Feb 09 '16 at 04:49