I am trying to change the background color of the search bar text field and i have searched and tried lots of solutions but those are not working.
So, please anyone can tell me the exact solution for that. How can we change the background color of the search bar text field?
/// used to prepare searchController inside navigation.
private func prepareNavigationSearchControllerSetup() {
self.title = AppConstant.kContacts
let search = UISearchController(searchResultsController: nil)
search.searchResultsUpdater = self
search.searchBar.cornerRadius = 10.0
search.searchBar.textField?.backgroundColor = .red
search.searchBar.textField?.tintColor = .yellow
self.navigationItem.searchController = search
self.navigationItem.hidesSearchBarWhenScrolling = false
}
extension UISearchBar {
var textField: UITextField? {
let subViews = subviews.flatMap { $0.subviews }
return (subViews.filter { $0 is UITextField }).first as? UITextField
}
}
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;
}
}
}
You can try this
UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).backgroundColor = .yellow
UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).tintColor = .blue
Output
Edit : Full Code
var searchController = UISearchController()
let resultsTableController = Storyboard.Home.instantiateViewController(withIdentifier: "GlobalTableVC") as! GlobalTableVC
resultsTableController.tableView.delegate = self
resultsTableController.tableView.dataSource = self
resultsTableController.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "SearchCell")
searchController = UISearchController(searchResultsController: resultsTableController)
searchController.searchBar.placeholder = "Search"
searchController.dimsBackgroundDuringPresentation = true
searchController.searchBar.sizeToFit()
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.keyboardType = UIKeyboardType.alphabet
searchController.searchBar.tintColor = UIColor.white
searchController.searchBar.barTintColor = UIColor(hexString: "EB033B")
UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).backgroundColor = .yellow
UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).tintColor = .blue
searchController.searchBar.delegate = self
searchController.delegate = self
searchController.searchResultsUpdater = self
present(searchController, animated: true, completion: nil)
Now (iOS 13.X) UISearchBar directly we can access UISearchBar.searchTextField
so we can directly replace our private API implementation to public API.
Objective - C
searchBar.searchTextField.backgroundColor = [UIColor blueColor];
Swift
public extension UISearchBar {
func changeborderColor(){
self.searchTextPositionAdjustment = UIOffset.init(horizontal: 10.0, vertical: 0.0)
let divideCount: CGFloat = 255.0
if #available(iOS 13.0, *) {
self.searchTextField.backgroundColor = .red
var placeHolderText = " "
if let getplaceHolder = self.searchTextField.placeholder, !getplaceHolder.isEmpty {
//print(getplaceHolder)
placeHolderText = getplaceHolder
}
self.searchTextField.placeholder = placeHolderText
let placeholderAttributedString = NSMutableAttributedString(attributedString: self.searchTextField.attributedPlaceholder!)
placeholderAttributedString.addAttribute(.foregroundColor, value: UIColor.XTRMWhite, range: NSRange(location: 0, length: placeholderAttributedString.length))
self.searchTextField.attributedPlaceholder = placeholderAttributedString
self.searchTextField.textColor = UIColor(red: 129.0/divideCount, green: 170.0/divideCount, blue: 200.0/divideCount, alpha: 1.0)
//Magnifying glass
let glassIconView = searchTextField.leftView as? UIImageView
glassIconView?.image = glassIconView?.image?.withRenderingMode(.alwaysTemplate)
glassIconView?.tintColor = .white
DispatchQueue.main.async {
self.searchTextField.underlined()
}
}else{
if let searchTextField = self.value(forKey: "_searchField") as? UITextField, let clearButton = searchTextField.value(forKey: "_clearButton") as? UIButton {
// Create a template copy of the original button image
// Set the template image copy as the button image
// clearButton.setImage(clearButton.imageView?.image, for: .normal)
// Finally, set the image color
clearButton.tintColor = .white
if let textFieldInsideSearchBarLabel = searchTextField.value(forKey: "placeholderLabel") as? UILabel {
textFieldInsideSearchBarLabel.textColor = .white
}
searchTextField.backgroundColor = .red
searchTextField.textColor = UIColor(red: 129.0/divideCount, green: 170.0/divideCount, blue: 200.0/divideCount, alpha: 1.0)
//Magnifying glass
let glassIconView = searchTextField.leftView as? UIImageView
glassIconView?.image = glassIconView?.image?.withRenderingMode(.alwaysTemplate)
glassIconView?.tintColor = .white
DispatchQueue.main.async {
searchTextField.underlined()
}
}
}
}
}
access the method as like
@IBOutlet weak var searchCurrency: UISearchBar!{
didSet{
searchCurrency.delegate = self
searchCurrency.changeborderColor()
}
}
Since iOS 13:
if #available(iOS 13.0, *) {
searchController.searchBar.searchTextField.backgroundColor = .red
searchController.searchBar.searchTextField.tintColor = .yellow
}
来源:https://stackoverflow.com/questions/58127704/uisearchbarsearchfield-backgroundview-color