search bar color is not exact in seacrhDisplayController

56 views Asked by At

I have search bar in my app and the problem is when I click to start typing the search bar change color. I wanted to set it programatically (via self.seacrhDisplayController.searchBar), but it is read only. Any idea how to set exact color? Thanks

search bar in my view controller

search bar in search display controller

1

There are 1 answers

4
Eric García On

This is done with

self.searchDisplayController?.searchBar.barTintColor = UIColor.black
self.searchDisplayController?.searchBar.isTranslucent = false

If you're creating it with Storyboard, first make sure

  • SearchDisplayController referencing outlet is set to file owner
  • Search Bar referencing outlet is set to the Search Display Controller

EDIT - Added image and alpha solution

Need to remove the background image or set a background image that has your desired color.

enter image description here

self.searchDisplayController?.searchBar.backgroundImage = UIImage() or self.searchDisplayController?.searchBar.backgroundImage = UIImage(named: "search_background")

Note: If you want to get rid of the deprecated warning you have to create a UISearchController instead, this is not available with Storyboard

Useful UISearchController tutorial

Hope this helps.