UISearchBar 不会更改 iOS 12.1 的搜索图标和占位符颜色

Posted

技术标签:

【中文标题】UISearchBar 不会更改 iOS 12.1 的搜索图标和占位符颜色【英文标题】:UISearchBar not changing search icon and placeholder color for iOS 12.1 【发布时间】:2019-05-30 17:20:17 【问题描述】:

我已经尝试了各种方法来更改搜索图标和占位符文本搜索栏的颜色,但没有任何效果。谁能帮帮我!

这是我正在关注的搜索栏实现。

func configureSearchBar() 
    let searchController = UISearchController(searchResultsController: nil)
    searchController.searchBar.delegate = self

    searchController.searchBar.tintColor = .white
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.searchBarStyle = .default
    navigationItem.searchController = searchController

    let textFieldInsideSearchBar = searchController.searchBar.value(forKey: "searchField") as? UITextField
    textFieldInsideSearchBar?.attributedPlaceholder = NSAttributedString(string: "Suche", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])

    searchController.searchBar.setText(color: .white)
    searchController.searchBar.setTextField(color: UIColor.white.withAlphaComponent(0.2))
    searchController.searchBar.setPlaceholderText(color: .white)
    searchController.searchBar.setSearchImage(color: .white)
    searchController.searchBar.setClearButton(color: .white)


extension UISearchBar 

func getTextField() -> UITextField?  return value(forKey: "searchField") as? UITextField 
func setText(color: UIColor)  if let textField = getTextField()  textField.textColor = color  
func setPlaceholderText(color: UIColor)  getTextField()?.setPlaceholderText(color: color) 
func setClearButton(color: UIColor)  getTextField()?.setClearButton(color: color) 

func setTextField(color: UIColor) 
    guard let textField = getTextField() else  return 
    switch searchBarStyle 
    case .minimal:
        textField.backgroundColor = color
        textField.layer.backgroundColor = color.cgColor
        textField.layer.cornerRadius = 6
    case .prominent, .default: textField.backgroundColor = color
    @unknown default: break
    


func setSearchImage(color: UIColor) 
    guard let imageView = getTextField()?.leftView as? UIImageView else  return 
    imageView.tintColor = color
    imageView.image = imageView.image?.withRenderingMode(.alwaysTemplate)

【问题讨论】:

请检查:***.com/questions/13817330/…***.com/questions/35699859/… 这些解决方案都不起作用。我已经检查了很多解决方案。我觉得实现中存在限制颜色变化的问题。 我认为该代码适用于除 12.1 之外的所有版本,您可以检查一下吗? 我应该检查哪一个? 有问题的所有方法都应该在 12 和 11 版本中运行,只要尝试它是否运行良好,然后苹果会出现这个问题,当它解决它时,它会自动在您的应用中运行。 【参考方案1】:

试试这个方法,它会改变搜索栏占位符的颜色

     var searchTextField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField
        if searchTextField!.responds(to: #selector(getter: UITextField.attributedPlaceholder)) 
            let attributeDict = [NSForegroundColorAttributeName: UIColor.white]
            searchTextField!.attributedPlaceholder = NSAttributedString(string: "Search", attributes: attributeDict)
        

  extension UISearchBar


    func setMagnifyingGlassColorTo(color: UIColor)
    
        // Search Icon
        let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
        let glassIconView = textFieldInsideSearchBar?.leftView as? UIImageView
        glassIconView?.image = glassIconView?.image?.withRenderingMode(.alwaysTemplate)
        glassIconView?.tintColor = color
    

    func setClearButtonColorTo(color: UIColor)
    
        // Clear Button
        let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
        let crossIconView = textFieldInsideSearchBar?.value(forKey: "clearButton") as? UIButton
        crossIconView?.setImage(crossIconView?.currentImage?.withRenderingMode(.alwaysTemplate), for: .normal)
        crossIconView?.tintColor = color
    

    func setPlaceholderTextColorTo(color: UIColor)
    
        let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
        textFieldInsideSearchBar?.textColor = color
        let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel
        textFieldInsideSearchBarLabel?.textColor = color
    

【讨论】:

与我在上面的问题中分享的相同。

以上是关于UISearchBar 不会更改 iOS 12.1 的搜索图标和占位符颜色的主要内容,如果未能解决你的问题,请参考以下文章

如何在iOS上更改UISearchBar组件的内部背景颜色

在 iOS 8 中更改 UISearchBar 取消按钮文本

在 iOS 9+ 中更改 UISearchBar 文本颜色

如何在iOS7中更改UISearchBar的取消按钮的textColor?

如何在 iOS7 中更改 UISearchBar 的 inputView?

更改 UISearchBar 的条形颜色 - iOS