我的NavigationBar中有一个SearchBar。单击SearchBar时,它应显示在导航栏上的“后退”按钮上。
Link to what I want to achieve
所以我想隐藏后面的按钮,如上图所示,但我没有实现搜索栏与后退按钮重叠。任何人都可以请帮助
这是我的代码:
class FirstSearchTableViewController: UITableViewController, UISearchResultsUpdating {
let searchData = []
var filteredSearchData = [String]()
var resultSearchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.placeholder = "Search for persons"
controller.searchBar.sizeToFit()
controller.searchBar.searchBarStyle = UISearchBarStyle.Minimal
controller.searchBar.setValue("X", forKey: "_cancelButtonText")
(UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self])).tintColor = UIColor.grayColor()
self.navigationItem.titleView = controller.searchBar
controller.hidesNavigationBarDuringPresentation = false
return controller
})()
// Reload the table
self.tableView.reloadData()
self.tableView.backgroundColor = UIColor(red: 14/255.0, green: 23/255.0, blue: 38/255.0, alpha: 1)
self.tableView.separatorColor = UIColor(red: 60/255.0, green: 69/255.0, blue: 83/255.0, alpha: 1)
self.tableView.rowHeight = 80.0
self.navigationController?.navigationBar.topItem?.title = ""
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// 1
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// 2
if (self.resultSearchController.active) {
return self.filteredSearchData.count
}
else {
return self.searchData.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
// 3
if (self.resultSearchController.active) {
cell.textLabel?.text = filteredSearchData[indexPath.row]
return cell
}
else {
cell.textLabel?.text = searchData[indexPath.row] as? String
return cell
}
}
func updateSearchResultsForSearchController(searchController: UISearchController)
{
filteredSearchData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
let array = (searchData as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredSearchData = array as! [String]
self.tableView.reloadData()
}
}
答案 0 :(得分:1)
我认为这些代表可能对您有所帮助:
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
self.navigationItem.setHidesBackButton(true, animated:true);
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
self.navigationItem.setHidesBackButton(false, animated:true);
}
这个链接答案可能会给你一些指针see implementation of above methods in link aanswer