Cannot Change Search Bar Background Color

UISearchBarSearchField BackgroundView color

After a lot more search I found the correct answer that is working for me.

if #available(iOS 11.0, *) {
if let textfield = search.searchBar.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.blue

if let backgroundview = textfield.subviews.first {
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}

}

How to change color of Search Bar?

Try:

self.mySearchBar.barTintColor = [UIColor redColor];

How to change background color of the text field in the UISearchController?

Here is a an example on how to set the textField background.

class ViewController: UIViewController {

let searchController = UISearchController(searchResultsController: nil)

private lazy var searchTextField: UITextField? = { [unowned self] in
var textField: UITextField?
self.searchController.searchBar.subviews.forEach({ view in
view.subviews.forEach({ view in
if let view = view as? UITextField {
textField = view
}
})
})
return textField
}()

override func viewDidLoad() {
super.viewDidLoad()

searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Candies"
navigationItem.searchController = searchController
definesPresentationContext = true

if let bg = self.searchTextField?.subviews.first {
bg.backgroundColor = .green
bg.layer.cornerRadius = 10
bg.clipsToBounds = true
}
}
}

Result

Sample Image

IOS UISearchBar Background Color In iOS 9

Swift 3

To remove the background altogether, set backgroundImage to an empty image:

searchBar.backgroundImage = UIImage()

To set a custom background color, use barTintcolor property:

searchBar.barTintColor = .green

Changing the background color in a search bar

You can do something like this:

instantsearch.widgets.searchBox({
// ...
cssClasses: {
input: 'bg-black',
},
});

And then in your CSS file:

.bg-black {
background-color: black;
}

You're essentially asking InstantSearch to add an additional class called bg-black to the HTML input element it renders. Then in your CSS, you're targeting that CSS selector and adding styles to it.

More docs here: https://www.algolia.com/doc/api-reference/widgets/search-box/js/#widget-param-cssclasses



Related Topics



Leave a reply



Submit