Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding Cancel button on search bar in UISearchController

I'm trying to hide the Cancel button of the search bar in the UISearchController, but unfortunately setting the following in viewDidLoad() does not work:

override func viewDidLoad() {
    super.viewDidLoad()

    searchResultsTableController = UITableViewController()
    searchResultsTableController.tableView.delegate = self

    searchController = UISearchController(searchResultsController: searchResultsTableController)
    searchController.searchResultsUpdater = self
    searchController.searchBar.sizeToFit()
    searchResultsView.tableHeaderView = searchController.searchBar

    searchController.delegate = self
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.delegate = self

    searchController.searchBar.searchBarStyle = .Minimal
    searchController.searchBar.showsCancelButton = false

    definesPresentationContext = true
}

I have also tried using the above code in this delegate method:

func didPresentSearchController(searchController: UISearchController) {
    searchController.searchBar.showsCancelButton = false
}

This approach works but will show the Cancel button briefly before hiding it, which is not ideal. Any suggestions?

like image 730
Griffith Avatar asked Oct 20 '15 02:10

Griffith


5 Answers

I ended up subclassing both UISearchBar and UISearchController as suggested:

CustomSearchBar.swift

import UIKit

class CustomSearchBar: UISearchBar {

    override func layoutSubviews() {
        super.layoutSubviews()
        setShowsCancelButton(false, animated: false)
    }
}

CustomSearchController.swift

import UIKit

class CustomSearchController: UISearchController, UISearchBarDelegate {

    lazy var _searchBar: CustomSearchBar = {
        [unowned self] in
        let result = CustomSearchBar(frame: CGRectZero)
        result.delegate = self

        return result
    }()

    override var searchBar: UISearchBar {
        get {
            return _searchBar
        }
    }
}
like image 65
Griffith Avatar answered Nov 08 '22 02:11

Griffith


Rejoice! As of iOS 13, there is access to automaticallyShowsCancelButton on UISearchController. Set it to false to hide the cancel button.

like image 37
Ben Packard Avatar answered Nov 08 '22 02:11

Ben Packard


func didPresentSearchController(_ searchController: UISearchController) {
    searchController.searchBar.becomeFirstResponder()
    searchController.searchBar.showsCancelButton = true
}


func didDismissSearchController(_ searchController: UISearchController) {
    searchController.searchBar.showsCancelButton = false
}

In my case all the above solutions dint work. You need to show in didPresentSearchController and hide it in didDismissSearchController. Earlier I was just hiding in didDismissSearchController which was still showing Cancel button on cancelling.

like image 25
vinny Avatar answered Nov 08 '22 01:11

vinny


Hide the Cancel button in search bar delegate methods and set your delegate searchController.searchBar.delegate=self UISearchBarDelegate

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {

}

func searchBarTextDidEndEditing(searchBar: UISearchBar) {

}
like image 40
Mukesh Avatar answered Nov 08 '22 01:11

Mukesh


Try subclassing UISearchBar and implement:

override func layoutSubviews() {
   super.layoutSubviews()
   self.setShowsCancelButton(false, animated: false)
}

This SO thread may help you more in this direction.

like image 1
Abhinav Avatar answered Nov 08 '22 01:11

Abhinav