我正在尝试为UISearchbar
中的占位符文本设置颜色。现在,我正在关注代码。在iOS 13上,它不会将白色设置为占位符文本。在iOS 12上,它可以工作。似乎有些问题或iOS 13中的支持已被删除?
我进行了很多搜索,并尝试了几种解决方法,但没有用。我还尝试为文本字段设置属性文本颜色,但这也不会更改颜色。
有没有可行的解决方案?
class CustomSearchBar: UISearchBar {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func awakeFromNib() {
super.awakeFromNib()
self.sizeToFit()
// Search text colour change
let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.textColor = UIColor.white
// Search Placeholder text colour change
let placeHolderText = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel
placeHolderText?.textColor = UIColor.white // doesn't work
}
}
答案 0 :(得分:4)
将代码放入viewDidAppear中,viewDidLoad为时过早。然后您的占位符应更改为白色
searchController.searchBar.searchTextField.attributedPlaceholder = NSAttributedString(string: "Enter Search Here", attributes: [NSAttributedString.Key.foregroundColor : UIColor.white])
答案 1 :(得分:0)
var searchBar: UISearchBar!
if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
textfield.attributedPlaceholder = NSAttributedString(string: textfield.placeholder ?? "", attributes: [NSAttributedString.Key.foregroundColor : UIColor.white])
}
答案 2 :(得分:0)
请添加此扩展程序,然后尝试!
extension UISearchBar{
var placeholderLabel: UILabel? { return value(forKey: "placeholderLabel") as? UILabel }
func setPlaceholder(textColor: UIColor) {
guard let placeholderLabel = placeholderLabel else { return }
let label = Label(label: placeholderLabel, textColor: textColor)
placeholderLabel.removeFromSuperview() // To remove existing label. Otherwise it will overwrite it if called multiple times.
setValue(label, forKey: "placeholderLabel")
}
}
private extension UITextField {
private class Label: UILabel {
private var _textColor = UIColor.lightGray
override var textColor: UIColor! {
set { super.textColor = _textColor }
get { return _textColor }
}
init(label: UILabel, textColor: UIColor = .lightGray) {
_textColor = textColor
super.init(frame: label.frame)
self.text = label.text
self.font = label.font
}
required init?(coder: NSCoder) { super.init(coder: coder) }
}
用法
yourSearchBarObject.setPlaceholder(textColor: .red)
答案 3 :(得分:0)
在viewDidAppear中,如果iOS高于13.0,请使用searchBar.searchTextField.attributedPlaceholder
;如果iOS低于13.0,请使用let searchField = searchBar.value(forKey: "searchField") as! UITextField
。
var searchBar = UISearchBar()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if #available(iOS 13.0, *) {
searchBar.searchTextField.attributedPlaceholder = NSAttributedString(string: "Enter Search Here", attributes: [NSAttributedString.Key.foregroundColor : UIColor.white])
} else {
let searchField = searchBar.value(forKey: "searchField") as! UITextField
searchField.attributedPlaceholder = NSAttributedString(string: "Enter Search Here", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white2])
}
}
答案 4 :(得分:0)
您需要为 viewDidAppear 中的所有内容设置样式