简体   繁体   English

如何将UISearchController的searchBar设置为不是tableHeaderView或navigationItem.titleview的视图?

[英]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.

相关问题 如何设置navigationItem.titleView图像水平居中? - How to set navigationItem.titleView image horizontal center? 我如何将navigationItem.titleView放在左右栏按钮项的后面 - how do I have the navigationItem.titleView to be behind left and right bar button item 当NavigationItem.titleView和rightBarButtonItem占据大部分UINavigationBar时,如何阻止leftBarButtonItem消失? - How do I stop leftBarButtonItem from disappearing when navigationItem.titleView and rightBarButtonItem take up most of the UINavigationBar? iOS 11 navigationItem.titleView 宽度未设置 - iOS 11 navigationItem.titleView Width Not Set 以UINavigationControllers navigationItem.titleView的形式在自定义视图中加扰 - Crasher in a custom view as UINavigationControllers navigationItem.titleView 如何在屏幕之间保留navigationItem.titleView? - How to persist navigationItem.titleView between screen? 将更多按钮添加到navigationItem时,如何将我的navigationItem.titleView居中? - How can I center my navigationItem.titleView when I add more buttons to the navigationItem? 无法在navigationItem titleView中编辑UISearchController UISearchBar - Can't edit UISearchController UISearchBar in navigationItem titleView 将UIBarButtonItem添加到navigationItem.titleView吗? - Add UIBarButtonItem to navigationItem.titleView? 自定义navigationItem.titleView未设置 - Custom navigationItem.titleView not setting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM