简体   繁体   中英

UISearchController appears behind NavigationBar

I'm trying to show the UISearchController by tapping on the UIBarButtonItem. But the SearchBar appears behind the NavigationBar. If I set definesPresentationContext = false, then the SearchBar appears above the NavigationBar, but the transitions don't work.

    @IBAction func searchButtonPressed(_ sender: Any) {
        searchResultsController = self.storyboard?.instantiateViewController(withIdentifier: "SearchResultsController") as? SearchResultsController
        searchResultsController.tableView.delegate = self
        let searchController = UISearchController(searchResultsController: searchResultsController)
        searchController.searchResultsUpdater = searchResultsController
        searchController.hidesNavigationBarDuringPresentation = false
        searchController.searchBar.autocapitalizationType = .words
        definesPresentationContext = true
        present(searchController, animated: true)
    }

Before BarButtonItem tapped

After BarButtonItem tapped

View UI Hierarchy

Interface Builder

searchController.hidesNavigationBarDuringPresentation = false solve this problem, but I need the navigation bar to not be hidden

UPDATE:

Inserting the SearchBar inside the Navigation Bar is not suitable for my app https://imgur.com/a/74e5sSk

If you are using UITableViewController then you can do the following:

let searchController = UISearchController(searchResultsController: nil)
searchController.obscuresBackgroundDuringPresentation = false
searchController.definesPresentationContext = true

Then, you need to assign this searchController to navigationItem :

self.navigationItem.searchController = searchController

Now searchbar is going to appear when you pull the tableview and will look this way:

在此处输入图片说明

If you want to keep the search bar permanently, then do this:

self.navigationItem.hidesSearchBarWhenScrolling = false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM