ios - 在 iOS 11 中点击搜索栏显示结果 Controller

标签 ios swift uikit uisearchcontroller

我有一个 UITableViewController,它在导航栏中有一个 UISearchController。我使用不同的 UIViewController 作为结果 Controller 。当我点击搜索栏并开始输入时,结果 Controller 显示如下。

enter image description here

但是,我想在用户点击带有一些建议的搜索栏时立即显示结果 Controller ,例如 native Messages 应用程序。

该选项在 iOS 13+ 中可用,通过设置 showsSearchResultsControllerUISearchControllerDelegatepresentSearchController 方法中为真。 如何为 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/

相关文章:

ios - 如何快速使用自定义 View ?

ios - 如何根据滑出菜单导航到不同的 View Controller

ios - 带身份验证的 Swift Telnet 流

ios - 使用旧版本的 Xcode 上传应用程序。该应用程序可以在 iOS 10 设备上使用吗?

ios - UIImageView 的拉伸(stretch)属性有什么用。我如何使用此属性。?

ios - 如何在 Swift 中使用协议(protocol)初始化 UIViews 和 UIViewControllers

ios - 如何在IOS中的UITabBarController上强制RTL?

ios - 崩溃报告工具的实际工作原理

ios - MPNowPlayingInfoCenter 信息被 AVPlayer 覆盖

iphone - 从内部 View Controller 推送 View Controller