3

I have a UISearchBar to which I want to set a white background.

Since I want to set a custom border, I choose the minimal value for searchBarStyle.

I can obtain a white background by setting backgroundColor and borderStyle to the searchTextField (this field is only available in iOS 13, to access it safely in other iOS versions check here), as follows:

searchBar.searchTextField.borderStyle = .none
searchBar.searchTextField.backgroundColor = .white

However, this doesn't work in iOS 13, it seems that the background color is ignored when searchBarStyle value is minimal, but correctly works for default / prominent styles.

francybiga
  • 960
  • 1
  • 8
  • 16

1 Answers1

3

While trying to figure this out, I noticed that the background color was correctly applied when changing searchBarStyle at runtime using Reveal.

So I tried to toggle the `searchBarStyle in code after searchbar configuration and I managed to make it work!

func configureSearchBar() {

    // ... other searchbar customization code ....

    searchBar.searchTextField.borderStyle = .none
    searchBar.searchTextField.backgroundColor = .white

    if #available(iOS 13.0, *) {
        searchBarStyle = .prominent
        searchBarStyle = .minimal
    }
}}

I know this is a very fragile workaround but in my case it solved the problem.

francybiga
  • 960
  • 1
  • 8
  • 16