[英]Display results controller on tapping search bar in iOS 11
我有一個 UITableViewController,它在導航欄中有一個 UISearchController。 我使用不同的 UIViewController 作為結果 Controller。 當我點擊搜索欄並開始輸入時,結果 controller 如下所示。
但是,我想在用戶點擊帶有一些建議的搜索欄后立即顯示結果 controller,例如本機消息應用程序。
該選項在 iOS 13+ 中可用,方法是在UISearchControllerDelegate
的presentSearchController
方法中將showsSearchResultsController
設置為 true。 如何為 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 視圖被隱藏。 您可以通過將其隱藏屬性設置為 false 來使其可見。
這對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.