Search code examples
iosswiftuisearchbarios9uisearchcontroller

iOS 9 searchBar disappears from table header view when UISearchController is active


The structure:

View1 (click a button) -> present modally (MyModalView: UITableViewController)

MyModalView has UISearchController embedded. The searchBar of UISearchController is placed in MyModalView.tableView.tableHeaderView.

It's been working fine since iOS 8.0. However on iOS 9, the searchBar disappear when the UISearchController is active. Please take a look at theses pictures below

The modal view:modal view

UISearchController active on iOS 8:search bar in iOS 8

UISearchController active on iOS 9:search bar in iOS 9

The very standard code:

override func viewDidLoad() {
    super.viewDidLoad()

    // Dynamically create a search controller using anonymous function
    self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false

        controller.searchBar.sizeToFit()
        controller.searchBar.delegate = self

        self.tableView.tableHeaderView = controller.searchBar

        return controller
    })()

    // Auto sizing row & cell height
    self.tableView.estimatedRowHeight = 130
    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.definesPresentationContext = true

    // No footer for better presentation
    self.tableView.tableFooterView = UIView.init(frame: CGRectZero)
}

This issue also happens in iOS 9.1 beta...

Any idea / pointer would be deeply appreciated

Cheers.


Solution

  • It seems all of us got the same problem but they were solved in different ways. However none of the suggested answers worked for me :(. Nevertheless thank you all for your time.

    I got a solution that solved my problem. It is setting Extend Edges - Under Opaque Bars of my (MyModalView: UITableViewController) to true in the Storyboard using Interface Builder.

    In summary:

    MyModalView: UITableViewController, in Storyboard using Interface Builder has

    Extend Edges: - Under Top Bars ticked - Under Bottom Bars ticked - Under Opaque Bars ticked

    screenshot