class ay7aga: UIViewController {
let searchBar: UISearchBar = {
let searchBar = UISearchBar()
searchBar.placeholder = "Search"
//HERE
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = UIColor(hex: 0xebebeb, alpha: 0.5)
return searchBar
}()
}
or YOU can use this
extension UISearchBar {
func getTextField() -> UITextField? { return value(forKey: "searchField") as? UITextField }
func setTextFieldColor(color: UIColor) {
guard let textField = getTextField() else { return }
switch searchBarStyle {
case .minimal:
textField.layer.backgroundColor = color.cgColor
textField.layer.cornerRadius = 6
case .prominent, .default: textField.backgroundColor = color
@unknown default: break
}
}
}