未调用 UISearchController 委托方法,搜索栏无法成为第一响应者
UISeachController Delegate methods not called, searchbar can't become first responder
我很难让搜索控制器中的搜索栏成为第一响应者。我注意到没有调用委托方法,但是当我输入内容以过滤用户列表时,搜索栏会按预期工作。
搜索控制器的定义:
lazy var searchController: UISearchController = {
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
return searchController
}()
正在设置:
private func setupSearchController() {
self.navigationItem.searchController = searchController
searchController.definesPresentationContext = true
searchController.delegate = self
searchController.isActive = true
searchController.searchBar.delegate = self
searchController.searchBar.becomeFirstResponder()
}
我尝试了另一个 SO 问题中的这个建议,但没有调用委托方法:
func didPresentSearchController(searchController: UISearchController) {
UIView.animate(withDuration: 0.1, animations: { () -> Void in }) { (completed) -> Void in
searchController.searchBar.becomeFirstResponder()
}
}
问题是您试图在 UI 完全加载之前访问 UI 元素(searchbarcontroller)。
这可以通过两种方式完成
使用主队列显示键盘
private func setupSearchController() {
self.navigationItem.searchController = searchController
searchController.definesPresentationContext = true
searchController.delegate = self
searchController.isActive = true
searchController.searchBar.delegate = self
DispatchQueue.main.async {
self.searchController.searchBar.becomeFirstResponder()
}
}
使用这种方法,键盘只会在 viewDidLoad 中显示一次
在 viewDidAppear 中显示小键盘
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.main.async {
self.searchController.searchBar.becomeFirstResponder()
}
}
使用这种方法,只要屏幕出现,键盘就会始终显示。
我很难让搜索控制器中的搜索栏成为第一响应者。我注意到没有调用委托方法,但是当我输入内容以过滤用户列表时,搜索栏会按预期工作。
搜索控制器的定义:
lazy var searchController: UISearchController = {
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
return searchController
}()
正在设置:
private func setupSearchController() {
self.navigationItem.searchController = searchController
searchController.definesPresentationContext = true
searchController.delegate = self
searchController.isActive = true
searchController.searchBar.delegate = self
searchController.searchBar.becomeFirstResponder()
}
我尝试了另一个 SO 问题中的这个建议,但没有调用委托方法:
func didPresentSearchController(searchController: UISearchController) {
UIView.animate(withDuration: 0.1, animations: { () -> Void in }) { (completed) -> Void in
searchController.searchBar.becomeFirstResponder()
}
}
问题是您试图在 UI 完全加载之前访问 UI 元素(searchbarcontroller)。 这可以通过两种方式完成
使用主队列显示键盘
private func setupSearchController() { self.navigationItem.searchController = searchController searchController.definesPresentationContext = true searchController.delegate = self searchController.isActive = true searchController.searchBar.delegate = self DispatchQueue.main.async { self.searchController.searchBar.becomeFirstResponder() } }
使用这种方法,键盘只会在 viewDidLoad 中显示一次
在 viewDidAppear 中显示小键盘
override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) DispatchQueue.main.async { self.searchController.searchBar.becomeFirstResponder() } }
使用这种方法,只要屏幕出现,键盘就会始终显示。