ios - 如何将搜索栏集中在 barbutton 点击上?
问题描述
当我单击栏按钮时,我正在尝试自动聚焦搜索栏,但它似乎不适用于becomeFirstResponder
,请按照 GIF:
我已经尝试过使用becomeFirstResponder
但它不起作用,这就是我处理搜索栏的方式:
// MARK: SearchBar Delegate
@IBAction func showSearchBarAction(_ sender: Any) {
let searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
searchController.searchBar.keyboardAppearance = .dark
searchController.dimsBackgroundDuringPresentation = false
navigationItem.searchController = searchController
navigationItem.searchController?.isActive = true
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
navigationItem.searchController?.isActive = false
self.navigationItem.searchController = nil
}
所有这些都完美无缺,但自动对焦是我真正想要的,我也无法改变我的设计
解决方案
此代码是解决方案:
这就是我的类声明的样子:
class YourViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate {
// MARK: SearchBar Delegate
@IBAction func showSearchBarAction(_ sender: Any) {
let searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
searchController.searchBar.keyboardAppearance = .dark
searchController.dimsBackgroundDuringPresentation = false
self.navigationItem.searchController = searchController
self.navigationItem.searchController?.isActive = true
}
func didPresentSearchController(_ searchController: UISearchController) {
DispatchQueue.main.async {
searchController.searchBar.becomeFirstResponder()
}
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
navigationItem.searchController?.isActive = false
self.navigationItem.searchController = nil
}
解决方案
推荐阅读
- html - django:如何将数据库中的日期值放入“input type='date'”?
- javascript - 静态html中没有src的嵌入字体
- python - 如何从一列列表中创建一个包含元素总数的新列
- javascript - 数据表 - 如何删除下拉列过滤器中的空白选择器
- java - Open api 3 + Spring cloud gateway + Micronaut Rest api
- javascript - 在控制台中出现错误 Uncaught ReferenceError: fnSubmit is not defined at HTMLInputElement.onclick
- terminal - 用 oh my zsh 在 WSL2 Ubuntu 中更改 LSCOLORS 没有任何作用
- python - 在 vscode 实时服务器上运行但不在烧瓶上运行的 Html 文件
- c++ - 如何确保多线程分别在两个无锁队列上推送两个值(在一个原子操作中)而不在 C++ 中使用锁?
- oracle - 当我尝试使用 BI Publisher 将数据加载到 word 中时,我收到 Message39 错误