我正在尝试更改搜索栏文本字段的背景色,我已经搜索并尝试了很多解决方案,但这些都不起作用。
所以,请大家告诉我确切的解决方法。如何更改搜索栏文本字段的背景色?
/// 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
}
}
最佳答案
经过多次搜索,我找到了对我有用的正确答案。
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;
}
}
}
关于ios - UISearchBarSearchField BackgroundView颜色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58127704/