UISearchBar not changing search icon and placehold

2019-08-24 04:55发布

问题:

I have tried every approach to change the color of search icon and placeholder text search bar but nothing worked. Can anyone please help me out!

This is the implementation I am following for search bar.

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

}

回答1:

Try this way it change the seachbar placeholder color

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

OR

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