Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Presenting UISearchController programmatically

I am presenting a UISearchController programmatically, without adding it to the navigationItem. The Calendar app does something similar.

Without a presentation context, the search bar appears correctly, but persists after pushing another view controller.

Without presentation context

This is expected, so we need to set definesPresentationContext on the list view controller... But that causes the search bar to render incorrectly.

With presentation context

Here's the code for context:

private lazy var searchController: UISearchController! = {
    let searchController = UISearchController(searchResultsController: nil)
    searchController.obscuresBackgroundDuringPresentation = false

    // If this is set to true, the search bar animates correctly, but that's
    // not the effect I'm after. See the next video.
    searchController.hidesNavigationBarDuringPresentation = false
    return searchController
}()

override func viewDidLoad() {
    super.viewDidLoad()
    definesPresentationContext = true
    searchButton.rx.tap.subscribe(onNext: { [unowned self] in
        present(searchController, animated: true)
    }).disposed(by: disposeBag)
}

Setting hidesNavigationBarDuringPresentation kind of fixes it, but we lose the tab bar, and the whole thing just looks bad.

with hidesNavigationBarDuringPresentation

I tried this solution (Unable to present a UISearchController), but it didn't help.

Any suggestions?

UPDATE: The issue is, more specifically, that the search bar appears behind the translucent navigation bar. Making the nav bar solid ( navigationController?.navigationBar.isTranslucent = false) makes the search bar appear under the nav bar.

like image 703
alekop Avatar asked Oct 17 '25 11:10

alekop


1 Answers

I have the same problem not been able to solve this either. It seems like the problem is that either

a) the searchcontroller is presented at the very top of the viewcontroller stack, even above the navigation controller, so that it stays active into the next viewcontroller push. or,

b) the searchcontroller is presented underneath the navigationcontroller so that it remains covered by the navigation bar

One idea: don't embed the viewcontroller which is presenting the searchcontroller in a navigation controller. instead, just create a UIView which looks like a navigation bar a the top. would this be an inappropriate solution?

like image 132
Plato Avatar answered Oct 19 '25 01:10

Plato



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!