How to put search bar in navigation bar programmatically? - Swift

时间:2018-02-03 11:06:09

标签: ios swift uisearchbar swift4

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

view with searchbar

I added a search bar in homeViewController.

HomeViewController

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

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

1 个答案:

答案 0 :(得分:4)

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

ViewController

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

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
    }
}

enter image description here