简体   繁体   English

更改 iOS 13 中的搜索栏占位符文本颜色

[英]Changing Search Bar placeholder text colour in iOS 13

用户界面搜索栏

I'm trying to set colour for placeholder text inside UISearchbar .我正在尝试为UISearchbar内的占位符文本设置颜色。 Right now I've following code.现在我有以下代码。 It doesn't set white colour to placeholder text on iOS 13. It works on iOS 12. It seems something is either broken or support has been removed in iOS 13?它不会将白色设置为 iOS 13 上的占位符文本。它适用于 iOS 12。似乎在 iOS 13 中某些内容已损坏或已删除支持?

I've searched a lot and tried few workarounds but doesn't work.我已经搜索了很多并尝试了一些解决方法,但没有奏效。 I've also tried to set attributed text colour for textfield but that also doesn't change colour.我也尝试为文本字段设置属性文本颜色,但这也不会改变颜色。

Is there a working solution for this?有没有可行的解决方案?

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
    }
}

viewDidLoad is too early viewDidLoad 为时过早

Put the code in viewDidAppear, viewDidLoad is too early.把代码放在viewDidAppear里,viewDidLoad太早了。 Then your placeholder should change to white然后你的占位符应该变成白色

searchController.searchBar.searchTextField.attributedPlaceholder = NSAttributedString(string: "Enter Search Here", attributes: [NSAttributedString.Key.foregroundColor : UIColor.white])

Try this:尝试这个:

var searchBar: UISearchBar!
        if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
            textfield.attributedPlaceholder = NSAttributedString(string: textfield.placeholder ?? "", attributes: [NSAttributedString.Key.foregroundColor : UIColor.white])
        }

Please add this extension and try this !请添加这个扩展并试试这个!

 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) }
}

Usage用法

yourSearchBarObject.setPlaceholder(textColor: .red)

In viewDidAppear, use searchBar.searchTextField.attributedPlaceholder if iOS is higher than 13.0 and use let searchField = searchBar.value(forKey: "searchField") as! UITextField在 viewDidAppear 中,如果 iOS 高于 13.0 则使用searchBar.searchTextField.attributedPlaceholder并使用let searchField = searchBar.value(forKey: "searchField") as! UITextField let searchField = searchBar.value(forKey: "searchField") as! UITextField if iOS is lower than 13.0. let searchField = searchBar.value(forKey: "searchField") as! UITextField如果 iOS 低于 13.0。

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])
   }
}

You need to style everything in viewDidAppear您需要为 viewDidAppear 中的所有内容设置样式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM