[英]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.
目前我将它作为标题放在一个tableview中,它可以正常工作,当然,当你走下桌子时,搜索栏会滚动离开屏幕。 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? 如何在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
For instance an outlet to a UISearchBar, or a blank UIView, or something like that. 例如,UISearchBar的出口,或空白的UIView,或类似的东西。
But it doesn't show the searchBar if I do that. 但如果我这样做,它不会显示searchBar。 It seems to only work as the header view of a table or as a navigationItem.titleView.
它似乎只能用作表的标题视图或navigationItem.titleView。
Am I missing something? 我错过了什么吗?
If you have a blank UIView that is placed above the tableview. 如果你有一个空白的UIView放在tableview上面。
let's assume you have an outlet to that blank UIView called searchContainer
. 让我们假设您有一个名为
searchContainer
空白UIView的searchContainer
。 Then you can add the search bar of the UISearchController
to that view by adding the following line 然后,您可以通过添加以下行将
UISearchController
的搜索栏添加到该视图
searchContainer.addSubview(searchController.searchBar)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.