我有一个 UITableViewController,它在导航栏中有一个 UISearchController。我使用不同的 UIViewController 作为结果 Controller 。当我点击搜索栏并开始输入时,结果 Controller 显示如下。
但是,我想在用户点击带有一些建议的搜索栏时立即显示结果 Controller ,例如 native Messages 应用程序。
该选项在 iOS 13+ 中可用,通过设置 showsSearchResultsController
在 UISearchControllerDelegate
的 presentSearchController
方法中为真。
如何为 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
}
最初,结果 Controller View 是隐藏的。您可以通过将其隐藏属性设置为 false 使其可见。
这对我有用。
关于ios - 在 iOS 11 中点击搜索栏显示结果 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62165184/