简体   繁体   中英

Hide Cancel Button on UISearchController swift 2.0

theres any way to hide the cancel button on UISearchController? 在此处输入图片说明

The other behaviour i would like to know if its possible, is when the user press the Cancel Button, to set the text on the UISearchBar.

Thanks

Using seachController.searchBar.setShowsCancelButton(false, animated:false) still seems to not do it for this.

Try adding the UISearchBarDelegate to your controller and assign the searchBar's delegate to self. Then you can use the searchBarShouldBeginEditing to edit its visibility.

    import UIKit

    class TableViewController: UITableViewController, UISearchBarDelegate {

    var searchController: UISearchController!
    let menuItems: [String] = ["Row 1", "Row 2", "Row 3"]

    override func viewDidLoad() {
        super.viewDidLoad()
        let searchResultsController = storyboard!.instantiateViewControllerWithIdentifier("SearchResultsViewControllerStoryboardIdentifier") as! SearchResultsTableViewController


        searchController = UISearchController(searchResultsController: searchResultsController)
        searchController.hidesNavigationBarDuringPresentation = false
        searchController.searchBar.delegate = self
    }

    @IBAction func searchButtonPressed(sender: UIBarButtonItem) {
        presentViewController(searchController, animated: true, completion: nil)
    }

    func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
        searchController.searchBar.setShowsCancelButton(false, animated: true)

        return true
    }

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

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return menuItems.count
    }

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

        cell.textLabel?.text = self.menuItems[indexPath.row]

        return cell
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM