【问题标题】:How can I prevent UISearchController dismiss when cancel button pressed?按下取消按钮时如何防止 UISearchController 关闭?
【发布时间】:2018-11-01 06:02:46
【问题描述】:
我只想在用户第一次按下取消按钮时隐藏键盘。
就像 AppStore.app 一样
我像这样使用 UISearchController:
navigationItem.searchController = searchController
============== 更新==============
这个取消按钮由 UISearchController 托管。
【问题讨论】:
标签:
swift
uisearchbar
uisearchcontroller
【解决方案1】:
如果您以编程方式进行搜索栏,您可以在 ViewController 上添加扩展,或者只使用如下函数调用。
extension ViewController: UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if let text = searchBar.text {
self.filterContent(text: text)
searchBar.resignFirstResponder()
}
}
}