首页 > 解决方案 > 在 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()
    }
}

标签: iosswiftuikituisearchcontroller

解决方案


我不确定这是一个好方法,但是您可以按照以下方式进行

func updateSearchResults(for searchController: UISearchController) {
    searchController.searchResultsController?.view.isHidden = false
}

最初,结果控制器视图是隐藏的。您可以通过将其隐藏属性设置为 false 来使其可见。

这对我有用。


推荐阅读