我想以编程方式将 UISearchBar
放入 UINavigationController
,而不是替换 UINavBar
标题,因为 我想要大标题出现。请参阅 iOS 上的文件应用程序。
这是我的.swift
搜索类:UITableViewController、UISearchBarDelegate
override func viewDidLoad() {
super.viewDidLoad()
searchBar()
}
func searchBar() {
let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
navigationItem.searchController?.searchBar.placeholder = "search".localized
navigationItem.searchController?.dimsBackgroundDuringPresentation = false
navigationItem.hidesSearchBarWhenScrolling = false
}
重点是:通过将 nil
更改为实际的 searchResultsController
,在搜索栏中键入文本后,整个导航 Controller (带有搜索栏和键入的文本)被替换为 searchResultsController
View 。 输入简短内容时没有搜索栏。
我正在努力在应用程序中使用 iOS 11/12 native 外观,将搜索栏放入 TableViewController
是不可能的。有人可以帮助我吗?
最佳答案
如果您不需要不同的 View Controller 来显示结果,只需使用(它不会使您的导航栏消失):
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
此后不要更改该值。
你的大部分代码都很好,但是你需要实现 UISearchResultsUpdating 协议(protocol)来检测更新(当用户在搜索栏中写入时)并使用 TableView (在你的 View Controller 内)来显示搜索结果。 您可以通过在函数内重新加载 TableView 的数据来做到这一点:updateSearchResults(for searchController:) 在用搜索栏内的文本过滤您想要的数据之后。你可以读一下这篇好文章tutorial有关该主题的更多信息。
关于ios - UINavigationController 中的 UISearchBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51469253/