如何将UISearchController的searchBar设置为不是tableHeaderView或navigationItem.titleview的视图? [英] How do I set the UISearchController's searchBar to a view that isn't the tableHeaderView or navigationItem.titleview?
问题描述
我正在尝试在表格滚动时将搜索栏保持在视图中。目前我将它作为标题放在一个tableview中,它可以正常工作,但当你向下走时,搜索栏会滚动离开屏幕。我以为我可以这样做只是修改这个代码示例:
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:
如何在iOS 8中使用UISearchController,其中UISearchBar位于我的导航栏中并具有范围按钮?
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
例如UISearchBar的出口,或空白的UIView,或类似的东西。
For instance an outlet to a UISearchBar, or a blank UIView, or something like that.
但如果我这样做,它就不会显示searchBar。它似乎只作为表的标题视图或navigationItem.titleView。
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.
我错过了什么?
推荐答案
如果你有一个空白的UIView放在桌面视图上方。
If you have a blank UIView that is placed above the tableview.
让我们假设你有一个出口空白UIView称为 searchContainer
。
然后你可以通过添加以下行将 UISearchController
的搜索栏添加到该视图
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)
这篇关于如何将UISearchController的searchBar设置为不是tableHeaderView或navigationItem.titleview的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!