iphone - 使用 searchBar 时隐藏后退按钮

标签 iphone swift uinavigationbar ios9 uisearchbar

我的 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/

相关文章:

iphone - 应用程序可以在 iPhone 上运行,但不能在 iPod Touch 上运行

swift - WKWebView 截图

swift - 您可以在多个应用程序之间使用 NSPersistentCloudKitContainer 共享数据吗?

ios - WriteToFile 不工作 swift

ios - 尝试添加 UINavigationBar 按钮时发生崩溃

iphone - SOAP 和 IOS 开发

iphone - 导出合规性如果我使用 CCCrypt

ios - 将导航堆栈中每个 ViewController 的导航栏色调/颜色更改为不同的值

iphone - 如何从“顶点缓冲对象”绘制单个三角形并设置其颜色?

iOS 导航标题不显示在子页面上