ios - 在 iOS 11 中点击搜索栏时显示结果控制器
问题描述
我有一个 UITableViewController,它在导航栏中有一个 UISearchController。我使用不同的 UIViewController 作为结果控制器。当我点击搜索栏并开始输入时,结果控制器如下所示。
但是,我想在用户点击带有一些建议的搜索栏时立即显示结果控制器,例如本机消息应用程序。
该选项在 iOS 13+ 中可用,showsSearchResultsController
方法presentSearchController
是在UISearchControllerDelegate
. 如何为 iOS 11+ 实现相同的功能?
完整代码:
import UIKit
class SearchViewController: UITableViewController {
lazy var resultsController = ResultsVC()
lazy var searchController = UISearchController(searchResultsController: resultsController)
override func viewDidLoad() {
super.viewDidLoad()
configureSearchController()
navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.title = "Search VC"
}
func configureSearchController() {
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = true
searchController.obscuresBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = true
searchController.searchResultsUpdater = resultsController
searchController.delegate = self
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
navigationController?.pushViewController(SearchViewController(), animated: true)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "Search cell"
return cell
}
}
extension SearchViewController: UISearchControllerDelegate {
func presentSearchController(_ searchController: UISearchController) {
if #available(iOS 13.0, *) {
searchController.showsSearchResultsController = true
} else {
// TODO:- show results controller
}
resultsController.showingSuggestions = true
}
}
class ResultsVC: UITableViewController {
lazy var showingSuggestions = false
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
navigationController?.pushViewController(SearchViewController(), animated: true)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = showingSuggestions ? "Suggestion cell" : "Result cell"
return cell
}
}
extension ResultsVC: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
if let searchText = searchController.searchBar.text,
!searchText.isEmpty {
showingSuggestions = false
} else {
showingSuggestions = true
}
tableView.reloadData()
}
}
解决方案
我不确定这是一个好方法,但是您可以按照以下方式进行
func updateSearchResults(for searchController: UISearchController) {
searchController.searchResultsController?.view.isHidden = false
}
最初,结果控制器视图是隐藏的。您可以通过将其隐藏属性设置为 false 来使其可见。
这对我有用。
推荐阅读
- python - 我们如何让我们的玩家在pygame中跳转
- django - 新手需要帮助理解 Docker Postgres django.db.utils.OperationalError: FATAL: password authentication failed for user "postgres"
- javascript - MongoDB 查找并映射 2 个结果数组
- python - 如何使用具有 1:N 输入/输出的 Triton 服务器“集成模型”从大图像创建补丁?
- api - 必须将响应数据作为空手道中的 Json 用于另一个请求
- android - Kotlin-flow:只能在协程体内调用暂停函数
- css - 在fluidRow中跨列垂直对齐项目
- python - 读取 zip 文件中的 docx 文件而不提取它
- postgresql - PostgreSQL 将密码加密从 SCRAM 降级为 md5
- c++ - Oracle SSL 身份验证钱包不再工作