2016-04-14 103 views
0

我在NavigationBarSearchController一起使用時出現問題。Swift - 與SearchController的導航欄問題

如果NavigationBartranslucent property被設置爲false NavigationBar超出屏幕的時候SearchController處於活動狀態。如果translucent property設置爲true,則它可以正常工作。

我該如何解決?

代碼及以下圖片:

import UIKit 

class SelecionaPaisTableViewController: UITableViewController, UISearchResultsUpdating { 

    //MARK: - Propriedades 
    var paises = [PaisCodigo]() 
    var paisesFiltrado = [PaisCodigo]() 

    var controladorDeBusca: UISearchController! 

    //MARK: - Métodos reescritos da View 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     //Dados dos países 
     carregaDadosPaises() 

     //Carrega configuração do SearchController 
     configurarControladorDeBusca() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    // MARK: - Métodos reescritos da Table view data source 
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if controladorDeBusca.active { 
      return paisesFiltrado.count 
     } else { 
      return paises.count 
     } 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("PaisCell", forIndexPath: indexPath) 
     //let cell = tableView.dequeueReusableCellWithIdentifier("PaisCell", forIndexPath: indexPath) as UITableViewCell 
     let pais: PaisCodigo 

     if controladorDeBusca.active { 
      pais = paisesFiltrado[indexPath.row] 
     } else { 
      pais = paises[indexPath.row] 
     } 

     cell.textLabel?.text = pais.nome + " (+" + String(pais.codigo) + ")" 

     if pais.nome != pais.nomeIngles { 
      cell.detailTextLabel?.text = pais.nomeIngles 
     } else { 
      cell.detailTextLabel?.text = "" 
     } 

     return cell 
    } 

    //MARK: - Métodos do UISearchResultsUpdating 
    func updateSearchResultsForSearchController(searchController: UISearchController) { 
     //paisesFiltrado.removeAll(keepCapacity: false) 
    } 

    //MARK: - Métodos 
    func carregaDadosPaises() { 
     let pais1 = PaisCodigo(nome: "Brasil", nomeIngles: "Brazil", codigo: 55) 
     let pais2 = PaisCodigo(nome: "United States", nomeIngles: "United States", codigo: 1) 

     paises += [pais1, pais2] 

     //paisesTableView.reloadData() 
    } 

    func configurarControladorDeBusca() { 
     //Configura Controlador de Busca 
     controladorDeBusca = UISearchController(searchResultsController: nil) 
     controladorDeBusca.searchResultsUpdater = self 
     controladorDeBusca.dimsBackgroundDuringPresentation = false 
     definesPresentationContext = true 

     //Configura a barra do Controlador de busca 
     controladorDeBusca.searchBar.placeholder = "Search country" 
     controladorDeBusca.searchBar.sizeToFit() 
     controladorDeBusca.searchBar.barTintColor = navigationController?.navigationBar.barTintColor 
     controladorDeBusca.searchBar.translucent = true 

     //UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).tintColor = UIColor.whiteColor() 

     //let atts = [NSForegroundColorAttributeName: UIColor.whiteColor()] 

     let atts = [ 
      NSFontAttributeName: UIFont(name:"GillSans-Bold", size:16)!, 
      NSForegroundColorAttributeName: UIColor.whiteColor(), 
      NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue 
     ] 

     controladorDeBusca.searchBar.setScopeBarButtonTitleTextAttributes(atts, forState: .Normal) 

     //Adiciona a barra do Controlador de Busca a Table View 
     tableView.tableHeaderView = controladorDeBusca.searchBar 
    } 
} 

enter image description hereenter image description here

回答