如何以编程方式将搜索栏放在导航栏中?- 斯威夫特 [英] How to put search bar in navigation bar programmatically? - Swift

查看:28
本文介绍了如何以编程方式将搜索栏放在导航栏中?- 斯威夫特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这个库,我的视图如下图所示:

I'm using this library and my view looks like this pic:

我在 homeViewController 中添加了一个搜索栏.

I added a search bar in homeViewController.

HomeViewController

func createSearchBar(){
        searchbar.showsCancelButton = false
        searchbar.placeholder = "searchbartext".localized()
        searchbar.delegate = self
        self.navigationItem.titleView = searchbar
    }

我的问题是:如何在子视图控制器中使用这个搜索栏(在 HomeViewController 中)?

My question is: How can I use this searchbar (in HomeViewController) in child view controller?

推荐答案

您需要创建一个 SearchController 并将其设置为在另一个 TableViewController 中显示结果:

You need to create an SearchController and set it up to show the results in another TableViewController:

视图控制器

import UIKit

class ViewController: UIViewController, UISearchControllerDelegate {

    var controladorDeBusca: UISearchController!

    var resultsTableViewController: ResultsTableViewController?

    override func viewDidLoad() {
        super.viewDidLoad()

        resultsTableViewController = storyboard!.instantiateViewController(withIdentifier: "resultsTableViewController") as? ResultsTableViewController

        configurarControladorDeBusca()
    }

    func configurarControladorDeBusca() {

        controladorDeBusca = UISearchController(searchResultsController: resultsTableViewController)
        controladorDeBusca.delegate = self
        controladorDeBusca.searchResultsUpdater = resultsTableViewController
        controladorDeBusca.dimsBackgroundDuringPresentation = true
        definesPresentationContext = true

        controladorDeBusca.loadViewIfNeeded()

        //Configura a barra do Controlador de busca
        controladorDeBusca.searchBar.delegate = resultsTableViewController
        controladorDeBusca.hidesNavigationBarDuringPresentation = false
        controladorDeBusca.searchBar.placeholder = "Search place"
        controladorDeBusca.searchBar.sizeToFit()
        controladorDeBusca.searchBar.barTintColor = navigationController?.navigationBar.barTintColor
        controladorDeBusca.searchBar.tintColor = self.view.tintColor

        //Adiciona a barra do Controlador de Busca a barra do navegador
        navigationItem.titleView = controladorDeBusca.searchBar
    }
}

ResultsTableViewController

ResultsTableViewController

import UIKit

class ResultsTableViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {

    var array = ["Brazil", "Bolivia", "United States", "Canada", "England", "Germany", "France", "Portugal"]

    var arrayFilter = [String]()

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return arrayFilter.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell", for: indexPath)

        cell.textLabel?.text = arrayFilter[indexPath.row]

        return cell
    }


    func updateSearchResults(for searchController: UISearchController) {


    }

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

        arrayFilter.removeAll()

        if let text = searchBar.text {
            for string in array {
                if string.contains(text) {
                    arrayFilter.append(string)
                }
            }
        }

        tableView.reloadData()
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {

        arrayFilter.removeAll()
        tableView.reloadData()
    }

    func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {

        arrayFilter.removeAll()
        tableView.reloadData()

        return true
    }
}

这篇关于如何以编程方式将搜索栏放在导航栏中?- 斯威夫特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆