`let searchBar = UISearchBar()
searchBar.placeholder = ChatConstants.searchTitle
searchBar.backgroundColor = .white
searchBar.delegate = self
searchBar.vCornerRadius = 20
searchBar.vBorderWidth = 1
searchBar.vBorderColor = .icedGray
searchBar.searchBarStyle = .minimal
searchBar.searchTextField.backgroundColor = .white
searchBar.translatesAutoresizingMaskIntoConstraints = false
if let textField = searchBar.value(forKey: ExploreConstants.searchFieldKey) as? UITextField {
let whiteColor = UIColor.white
let attributedString = NSAttributedString(string: "Search", attributes: [NSAttributedString.Key.foregroundColor: whiteColor])
textField.attributedPlaceholder = attributedString
textField.font = UIFont.regularDMSans(of: ExploreConstants.textFieldFontSize)
textField.layer.cornerRadius = ExploreConstants.searchBarCornerRadius
if let searchIcon = UIImage(named: ExploreConstants.iconExploreSearch) {
let imageView = UIImageView(image: searchIcon)
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: ExploreConstants.searchIconOffset + searchIcon.size.width, height: searchIcon.size.height))
imageView.frame = CGRect(x: ExploreConstants.searchIconOffset, y: 0, width: searchIcon.size.width, height: searchIcon.size.height)
paddingView.addSubview(imageView)
textField.leftView = paddingView
textField.leftViewMode = .always
}
}`
}
I’m configuring the search bar as shown above. However, I couldn’t make the gray area seen in the image white. How can I change this area to white?
enter image description here
I expected that by changing the background color of the _UISearchBarSearchFieldBackgroundView to white, the gray area in the search bar would also turn white.
New contributor
ezgi özkan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.