与NavigationBar
一起使用SearchController
时遇到问题。
如果NavigationBar
translucent property
设置为false,则当NavigationBar
处于活动状态时,SearchController
会退出屏幕。如果translucent property
设置为true,则可以正常工作。
我该如何解决?
以下代码和图片:
Swift文件
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
}
}
答案 0 :(得分:0)
如果不使用translucent
NavigationBar
,您需要确保adjusts scroll view insets
上的extend edges under opaque bars
和ViewController
都设置为true。
答案的链接: Setting NavigationController's NavigationBar translucent property to false causes extra padding