Swift: searchBar - how to change "Cancel" Button title

Try accessing the button text using setValue, like this:

Swift 4

searchController.searchBar.setValue("New Title", forKey: "cancelButtonText")

it works for me :)


func searchDisplayControllerWillBeginSearch(controller: UISearchDisplayController) {
    self.searchDisplayController?.searchBar.showsCancelButton = true
    var cancelButton: UIButton
    var topView: UIView = self.searchDisplayController?.searchBar.subviews[0] as UIView
    for subView in topView.subviews {
        if subView.isKindOfClass(NSClassFromString("UINavigationButton")) {
            cancelButton = subView as UIButton
            cancelButton.setTitle("Vazgeç", forState: UIControlState.Normal)
        }
    }
}