【问题标题】:change color searchBar result icon swift快速更改颜色搜索栏结果图标
【发布时间】:2019-01-11 23:40:09
【问题描述】:

如何在swift中通过代码更改结果图标的颜色?

【问题讨论】:

  • 没用
  • 好的,分享你尝试过的代码!
  • let sb = UISearchBar() sb.searchBarStyle = UISearchBarStyle.minimal sb.showsSearchResultsButton = true // sb.setClearButtonColorTo(color: UIColor.white) let textFieldInsideSearchBar = sb.value(forKey: "searchField" ) 作为? UITextField 让 crossIconView = textFieldInsideSearchBar?.value(forKey: "clearButton") as? UIButton crossIconView?.setImage(crossIconView?.currentImage?.withRenderingMode(.alwaysTemplate), for: .normal) crossIconView?.tintColor = .white
  • 在 uiviewcontroller 我使用 addSubView(sb) 然后锚定它

标签: swift searchbar


【解决方案1】:

似乎currentImagenil for clearButton in Swift 4.2 and 4.1.x。它可能一直在旧版本中工作,因为在许多其他答案中它为他们工作。

所以我用Usage下显示的常见自定义创建了这个类。

class SearchBar: UISearchBar {

    private enum SubviewKey: String {
        case searchField, clearButton, cancelButton,  placeholderLabel
    }

    // Button/Icon images
    public var clearButtonImage: UIImage?
    public var resultsButtonImage: UIImage?
    public var searchImage: UIImage?

    // Button/Icon colors
    public var searchIconColor: UIColor?
    public var clearButtonColor: UIColor?
    public var cancelButtonColor: UIColor?
    public var capabilityButtonColor: UIColor?

    // Text
    public var textColor: UIColor?
    public var placeholderColor: UIColor?
    public var cancelTitle: String?

    // Cancel button to change the appearance.
    public var cancelButton: UIButton? {
        guard showsCancelButton else { return nil }
        return self.value(forKey: SubviewKey.cancelButton.rawValue) as? UIButton
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        if let cancelColor = cancelButtonColor {
            self.cancelButton?.setTitleColor(cancelColor, for: .normal)
        }
        if let cancelTitle = cancelTitle {
            self.cancelButton?.setTitle(cancelTitle, for: .normal)
        }

        guard let textField = self.value(forKey: SubviewKey.searchField.rawValue) as? UITextField else { return }

        if let clearButton = textField.value(forKey: SubviewKey.clearButton.rawValue) as? UIButton {
            update(button: clearButton, image: clearButtonImage, color: clearButtonColor)
        }
        if let resultsButton = textField.rightView as? UIButton {
            update(button: resultsButton, image: resultsButtonImage, color: capabilityButtonColor)
        }
        if let searchView = textField.leftView as? UIImageView {
            searchView.image = (searchImage ?? searchView.image)?.withRenderingMode(.alwaysTemplate)
            if let color = searchIconColor {
                searchView.tintColor = color
            }
        }
        if let placeholderLabel =  textField.value(forKey: SubviewKey.placeholderLabel.rawValue) as? UILabel,
            let color = placeholderColor {
            placeholderLabel.textColor = color
        }
        if let textColor = textColor  {
            textField.textColor = textColor
        }
    }

    private func update(button: UIButton, image: UIImage?, color: UIColor?) {
        let image = (image ?? button.currentImage)?.withRenderingMode(.alwaysTemplate)
        button.setImage(image, for: .normal)
        button.setImage(image, for: .highlighted)
        if let color = color {
            button.tintColor = color
        }
    }
}

用法:

class ViewController: UIViewController {

    @IBOutlet private weak var searchBar: SearchBar!

    override func viewDidLoad() {
        super.viewDidLoad()

        searchBar.clearButtonColor      = .purple
        searchBar.cancelButtonColor     = .magenta
        searchBar.searchIconColor       = .red
        searchBar.placeholderColor      = .green
        searchBar.textColor             = .orange
        searchBar.capabilityButtonColor = .green
    }
}

输出:

【讨论】:

  • 这应该是答案。你摇滚!谢谢!
【解决方案2】:
let sb = UISearchBar()

    sb.searchBarStyle = UISearchBarStyle.minimal
    sb.showsSearchResultsButton = true
   // sb.setClearButtonColorTo(color: UIColor.white)

    let textFieldInsideSearchBar = sb.value(forKey: "searchField") as? UITextField
    let crossIconView = textFieldInsideSearchBar?.value(forKey: "clearButton") as? UIButton
    crossIconView?.setImage(crossIconView?.currentImage?.withRenderingMode(.alwaysTemplate), for: .normal)
    crossIconView?.tintColor = .white

【讨论】:

    猜你喜欢
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多