I want to change the background color of a text field in searchBar
embedded in UISearchController in iOS 11, using the following code:
class ViewController: UIViewController {
private var searchController: UISearchController!
private var data: [String] = []
private var filtered: [String] = []
private var tableView = UITableView(frame: .zero, style: .plain)
override func viewDidLoad() {
super.viewDidLoad()
for i in 1...100 {
self.data.append("row \(i)")
}
self.filtered = data
let src = SearchResultController()
self.searchController = UISearchController(searchResultsController: src)
self.searchController.delegate = self
self.searchController.searchResultsUpdater = src
self.searchController.searchBar.textField?.subviews.first?.backgroundColor = UIColor(red:0.25, green:0.37, blue:0.53, alpha:1)
if #available(iOS 13.0, *) {
self.searchController.showsSearchResultsController = true
}
self.navigationItem.title = "Search"
self.navigationItem.hidesSearchBarWhenScrolling = false
self.navigationItem.searchController = searchController
self.setupNavigationBar()
self.setupTableView()
definesPresentationContext = true
}
}
So when I write code like this
self.searchController.searchBar.textField.backgroundColor = UIColor(red:0.25, green:0.37, blue:0.53, alpha:1)
nothing has changed.
when I write
self.searchController.searchBar.textField?.subviews.first?.backgroundColor = UIColor(red:0.25, green:0.37, blue:0.53, alpha:1)
I have something like this. The problem is that textfiled is too dark.
And when I write this. Alpha = 0.5
self.searchController.searchBar.textField?.subviews.first?.backgroundColor = UIColor(red:0.25, green:0.37, blue:0.53, alpha:0.5)
I have this. And this is what I want.
So my question is what I'm doing wrong ? And what the proper method to set background color of the textfiled in search bar embedded in UISearchController ?