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

Car*_*los 16 uisearchbar ios swift ios8 uisearchcontroller

当桌子滚动时,我试图将搜索栏保持在视图中.目前我将它作为标题放在一个tableview中,它可以正常工作,当然,当你走下桌子时,搜索栏会滚动离开屏幕.我以为我可以这样做只需修改此代码示例:

如何在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
Run Code Online (Sandbox Code Playgroud)

这个想法是采取其他观点并做

otherview = searchController.searchBar
Run Code Online (Sandbox Code Playgroud)

例如,UISearchBar的出口,或空白的UIView,或类似的东西.

但如果我这样做,它不会显示searchBar.它似乎只能用作表的标题视图或navigationItem.titleView.

我错过了什么吗?

Pra*_*I V 27

如果你有一个空白的UIView放在tableview上面.

让我们假设你有一个空白UIView的出口叫searchContainer.然后,您可以UISearchController通过添加以下行添加该视图的搜索栏

searchContainer.addSubview(searchController.searchBar)
Run Code Online (Sandbox Code Playgroud)