我的 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()
}
}
最佳答案
我认为这些代表可能会帮助你:
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
关于iphone - 使用 searchBar 时隐藏后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33789203/