首页 文章

如何设置UISearchController 's searchBar to a view that isn' t tableHeaderView或navigationItem.titleview?

提问于
浏览
14

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

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

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

otherview = searchController.searchBar

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

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

我错过了什么吗?

1 回答

  • 24

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

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

    searchContainer.addSubview(searchController.searchBar)
    

相关问题