Setting UISearchbar background color to white in IOS

Viewed 968

I am trying to change the background color of the UISearchbar. I am able to set the background to any color but white. Not sure why white does not get set. I am using the below code snippets to set the color. I have an extension method to get the textField. So, you can assume that textField exists.

searchBar.backgroundColor = .white
searchBar.textField?.backgroundColor = .white

It always has gray color when I set it to white. enter image description here

However, if I use a color other than white it shows up that color.

searchBar.textField?.backgroundColor = .blue

enter image description here

Any idea what is wrong here.

2 Answers

The only reliable way I have found to do this is to set the searchFieldText's .borderStyle to .none and manually set the layer's cornerRadius to match what the default border style would be. Something like:

self.searchBar.searchTextField.borderStyle = .none
self.searchBar.searchTextField.layer.cornerRadius = 10
self.searchBar.searchTextField.backgroundColor = .white

Try this one:

if #available(iOS 13.0, *) {
    searchController.searchBar.textField.backgroundColor = .white
}
Related