1

I've spent a lot of time in searching for a solution but couldn't find any which works and is efficient. As I understand we can't change tint colour of clear button as this property isn't available? The other solution is to change clear button image all together.

https://stackoverflow.com/a/50386999/10733471 this solution comes to the closest but problem with this is that it doesn't show clear button very first time you launch app and enter into search bar to type.

Can anyone suggest a working & efficient solution which doesn't involve using a new image? I'm looking for a solution which takes the default clear button image and reuses it after changing it's colour.

Search Bar Clear Image

Dan
  • 475
  • 9
  • 26
  • I'd suggest to iterate through all UISearchBar subviews, find UIButton instance and replace its image with something you need – Sergey Gamayunov Dec 13 '18 at 19:51
  • It might be not be the answer you want, but this kind of request probably exist since iOS7. The only way Apple address it is by providing an API to change the image. Why not stick to this option then? Any solution that implies something like using valueForKey to access a private variable is more likely to break in a future iOS release than something based on an official API. – Renaud Dec 14 '18 at 09:09
  • I just made a quick test: if you provide an image and choose "Template Image" for the "Render As" setting, it will respect the tint color of the search bar. – Renaud Dec 14 '18 at 09:24
  • searchBar.setImage(UIImage(named: "ic_clear"), for: .clear, state: .normal) is this not working ? with your custom image ? – Abu Ul Hassan Dec 14 '18 at 12:16

1 Answers1

0

Add the following code to change the color of clear button in Search Bar

  let imgClear = UIImage(named: "cross")
  searchbar.setImage(imgClear, for: .clear, state: .normal)

This code is working on first time also I have checked in sample project it is working perfectly