【问题标题】:Changing Search Bar placeholder text colour in iOS 13在 iOS 13 中更改搜索栏占位符文本颜色
【发布时间】:2020-02-22 16:41:11
【问题描述】:

我正在尝试为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
    }
}

【问题讨论】:

  • 您找到解决方案了吗?我在搜索的所有地方都在答案中找到了相同的代码。它不适用于占位符 textColor,而是更改了 searchBar 文本颜色。
  • 奇怪的是,仍然没有任何解决方案适合我。除了文本颜色(背景、阴影等)之外,每个属性都可以设置

标签: ios swift uisearchbar ios13 swift5


【解决方案1】:

viewDidLoad 太早了

把代码放到viewDidAppear里,viewDidLoad太早了。然后你的占位符应该变成白色

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

【讨论】:

  • 太棒了!到目前为止唯一对我有用的东西!!每个人都回答相同的代码,但没有人在任何地方提到这一点。非常感谢朋友的技巧!!这应该是被接受的答案。
【解决方案2】:

在viewDidAppear

如果 iOS 高于 13.0,请使用 searchBar.searchTextField.attributedPlaceholder。

如果iOS低于13.0,使用searchBar.value(forKey: "searchField")访问searchTextField

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 {
       if let searchField = searchBar.value(forKey: "searchField") as? UITextField {
           searchField.attributedPlaceholder = NSAttributedString(string: "Enter Search Here", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white2])
       }
   }
}

【讨论】:

    【解决方案3】:

    试试这个:

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

    【讨论】:

    • 我已经试过了,再试一次。不工作。正如您在我刚刚上传的图片中看到的那样,占位符文本颜色根本没有改变。
    • 这不再适用于 iOS 13。不过它适用于一些早期版本。
    【解决方案4】:

    请添加此扩展并尝试一下!

     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)
    

    【讨论】:

    • 使用未解析的标识符“标签”。这里的标签是什么?
    • 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[<.customuisearchbar> valueForUndefinedKey:]:此类不符合键 placeholderLabel 的键值编码。”
    【解决方案5】:

    你需要为 viewDidAppear 中的所有内容设置样式

    【讨论】:

    • 嗨!欢迎来到 SO。在您的答案中添加一些代码以使其清晰易懂
    猜你喜欢
    • 1970-01-01
    • 2020-01-06
    • 2014-12-14
    • 2017-05-18
    • 2013-02-04
    • 2020-01-09
    • 2013-05-12
    • 1970-01-01
    相关资源
    最近更新 更多