Reshma
Reshma

Reputation: 253

Setting UISearchbar background color to white in IOS

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.

Upvotes: 12

Views: 2636

Answers (2)

KerrM
KerrM

Reputation: 5230

The only reliable way I have found to do this is to set the searchTextField'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

Upvotes: 9

zeytin
zeytin

Reputation: 5643

Try this one:

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

Upvotes: 0

Related Questions