如何以编程方式将搜索栏放入导航栏? – 斯威夫特

我正在使用这个库,我的视图看起来像这张图:

用搜索栏查看

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

HomeViewController

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

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

您需要创建一个SearchController并将其设置为在另一个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

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

在此处输入图像描述