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

2024-03-08

我试图在表格滚动时保持搜索栏可见。目前,我将其作为表视图中的标题,并且它按应有的方式工作,但是当然,当您沿着表格向下滚动时,搜索栏会滚动到屏幕之外。我想我可以简单地修改这个代码示例来做到这一点:

如何在 iOS 8 中使用 UISearchController,其中 UISearchBar 位于导航栏中并具有范围按钮? https://stackoverflow.com/questions/27880031/how-do-i-use-uisearchcontroller-in-ios-8-where-the-uisearchbar-is-in-my-navigati

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,或类似的东西。

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

我错过了什么吗?


如果您有一个放置在表视图上方的空白 UIView。

假设您有一个名为的空白 UIView 的出口searchContainer。 然后你可以添加搜索栏UISearchController通过添加以下行到该视图

searchContainer.addSubview(searchController.searchBar)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 UISearchController 搜索栏设置为不是 tableHeaderView 或 navigationItem.titleview 的视图? 的相关文章

随机推荐