After looking for updated (iOS 13) answers, I didn't find any solutions to this simple problem :
How to change the textColor
of the placeholder in an UISearchBar
?
My app doesn't handle Light/Dark
mode. I don't want the system to change my UIPlaceHolder
text color. I want it to be always white
.
if #available(iOS 13.0, *) {
let attrString = NSMutableAttributedString(string: "My PlaceHolder")
attrString.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], range: NSRange(location: 0, length: attrString.length))
searchController.searchBar.searchTextField.attributedPlaceholder = attrString
}
I expected this code to work. I thought the new property searchTextField
would have made it easier to customize my UISearchBar
.
EDIT:
This code kind of works in the viewDidAppear
method :
if #available(iOS 13.0, *) {
searchController.searchBar.searchTextField.attributedPlaceholder = NSAttributedString(string: "My PlaceHolder", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white.withAlphaComponent(0.80)])
}
The issue is that the color is changing when you scroll up and down.