简体   繁体   中英

How do I set the UISearchController's searchBar to a view that isn't the tableHeaderView or navigationItem.titleview?

I'm trying to keep the search bar in view as the table scrolls. At the moment I'm placing it as the header in a tableview, and it works as it should, but of course the search bar scrolls off screen as you go down the table. I thought I could do this simply modifying this code sample:

How do I use UISearchController in iOS 8 where the UISearchBar is in my navigation bar and has scope buttons?

searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
tableview.tableHeaderView = searchContoller.searchBar // How to put it elsewhere?
//Alternative that also works
navigationItem.titleView = searchController.searchBar

The idea was to take some other view and do

otherview = searchController.searchBar

For instance an outlet to a UISearchBar, or a blank UIView, or something like that.

But it doesn't show the searchBar if I do that. It seems to only work as the header view of a table or as a navigationItem.titleView.

Am I missing something?

If you have a blank UIView that is placed above the tableview.

let's assume you have an outlet to that blank UIView called searchContainer . Then you can add the search bar of the UISearchController to that view by adding the following line

searchContainer.addSubview(searchController.searchBar)

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