Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
804 views
in Technique[技术] by (71.8m points)

swift - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm not sure what exactly is the problem but I 'fixed' it by:

self.searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = NO;

My guess is that UISearchController is doing something funky when it is trying to present as a navigation bar. So, this is a hack but it at least doesn't block the user. The search bar doesn't do the cool animation and cover up the navigation bar.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...