1

I want to customize the cancel button inside the UISearchBar, that appears when the user type a text, to clear the text, see the image, I want to change the tint color of the button inside the red square NOT the button with "cancel" text

What I have tried :

[[UIButton appearanceWhenContainedIn:[UISearchBar class], nil] setTintColor:[UIColor redColor]];

but it changes the color of the button with the text "cancel" not the icon to clear the text inside the textfield

it's not a duplicate question because I want to change the tint of the default clear image, NOT using a custom image, something like that :

How to change the tint color of the clear button on a UITextField

but for UISearchBar

enter image description here

Community
  • 1
  • 1
iOSGeek
  • 5,115
  • 9
  • 46
  • 74

2 Answers2

2
UIImage *imgClear = [UIImage imageNamed:@"clear"];
[mySearchBar setImage:imgClear forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
Shreyank
  • 1,549
  • 13
  • 24
  • 1
    That works, but I want to just change the tint color to white of the default clear button (because the background color of the text field is dark) – iOSGeek May 25 '16 at 11:44
  • check this answer for swift version: https://stackoverflow.com/a/52257939/3400939 – Kosrat D. Ahmad Sep 11 '18 at 08:01
1

Swift 4.2, 4.1+

It seems that the currentImage is nil for clearButton from Swift 4.1+. It might be working in the older versions.

So i created this class and the addition here is to provide your own image and color for clear button.

class SearchBar: UISearchBar {

    var clearButtonImage: UIImage?
    var clearButtonColor: UIColor = .white

    override func layoutSubviews() {
        super.layoutSubviews()

        if let textField = self.value(forKey: "searchField") as? UITextField {
            if let clearButton = textField.value(forKey: "clearButton") as? UIButton {
                update(button: clearButton, image: self.clearButtonImage, color: clearButtonColor)
            }
        }
    }

    private func update(button: UIButton, image: UIImage?, color: UIColor) {
        let image = (image ?? button.currentImage)?.withRenderingMode(.alwaysTemplate)
        button.setImage(image, for: .normal)
        button.setImage(image, for: .highlighted)
        button.tintColor = color
    }
}

So now you can set the clearButton image as below to make it work,

let searchBar = SearchBar()
searchBar.clearButtonImage = UIImage(named: "clearIcon")
searchBar.clearButtonColor = .green

Storyboard or XIB

You have to set the SearchBar class as custom class for search bar shown below,

enter image description here

And outlet as below to set the images and colors etc

@IBOutlet private weak var searchBar: SearchBar!
Kamran
  • 14,987
  • 4
  • 33
  • 51
  • 2
    The reason it is nil is because you need to have typed something at least once for it to been loaded into memory. So you need to update it on the first edit as well (or as you do, in layoutSubviews) – Claus Jørgensen May 23 '19 at 13:58