【问题标题】:UIButton tintColor for disabled and enabled state?禁用和启用状态的 UIButton tintColor?
【发布时间】:2017-08-23 08:50:54
【问题描述】:

我有一个继承自 UIButton 的自定义 class。我想要完成的事情是根据按钮的启用状态(即启用或禁用)设置tintColor 属性。

有什么方法可以实现吗?

这是我的课:

class ButtonsPostMenu: UIButton
{
    override func awakeFromNib()
    {
        titleLabel?.font = UIFont(name: Font_AvenirNext_Medium, size: 14)
        tintColor = UIColor.white
    }
}

【问题讨论】:

  • 不知道如何以编程方式做到这一点:你是如何以不同的方式完成的?
  • @EridB - 我选词不当。忽略它。

标签: ios swift uibutton


【解决方案1】:

您可以覆盖 isEnabled 属性来实现这一点。 tintColor 会根据按钮的 isEnabled 状态自动改变:

class ButtonsPostMenu:UIButton {

    //......

    override var isEnabled: Bool {
        didSet{
            if self.isEnabled {
                self.tintColor = UIColor.white
            }
            else{
                self.tintColor = UIColor.gray
            }
        }
    }

    //......

}

【讨论】:

  • 也用它来改变tintColor的高亮状态,覆盖isHighlighted
【解决方案2】:

这是你的课:添加changeStateOfButton自定义方法来管理UIButton的色调颜色

class ButtonsPostMenu: UIButton
{
    override func awakeFromNib()
    {
        titleLabel?.font = UIFont(name: Font_AvenirNext_Medium, size: 14)
        tintColor = UIColor.white
    }

    func changeStateOfButton() {
       if self.isEnabled {
           self.tintColor = UIColor.red // Set your color when button is enabled 
       }
       else {
           self.tintColor = UIColor.yellow // Set your color when button is disabled
       }
  }
}

只需调用上述方法,例如当您想根据启用/禁用 UIButton 设置颜色时。

【讨论】:

    【解决方案3】:

    您可以尝试像这样复制setX(_ x: X, forState: State) API:

    private class TintColorButton: UIButton {
    
        private var tintDict: [State.RawValue: UIColor] = [:]
        private var store: Set<AnyCancellable> = []
    
        override init(frame: CGRect) {
            super.init(frame: frame)
    
            publisher(for: \.isHighlighted).sink { [weak self] _ in self?.applyTintColor() }.store(in: &store)
            publisher(for: \.isEnabled).sink { [weak self] _ in self?.applyTintColor() }.store(in: &store)
        }
    
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        func setTintColor(_ color: UIColor?, forState state: State) {
            tintDict[state.rawValue] = color
            applyTintColor()
        }
    
        private func applyTintColor() {
            tintColor = tintDict[state.rawValue] ?? tintDict[State.normal.rawValue]
        }
    }
    

    【讨论】:

      【解决方案4】:
          func setBackgroundColor(color: UIColor, forState: UIControlState) {
      UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
      CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), color.CGColor)
      CGContextFillRect(UIGraphicsGetCurrentContext(), CGRect(x: 0, y: 0, width: 1, height: 1))
      let colorImage = UIGraphicsGetImageFromCurrentImageContext()
      UIGraphicsEndImageContext()
      
      self.setBackgroundImage(colorImage, forState: forState)
       }
      

      【讨论】:

        【解决方案5】:

        我认为图像过滤是一个不错的选择。

        btn.setImage(image, for: UIControl.State.normal)
        btn.setImage(image.disabled, for: UIControl.State.disabled)
        

        使用CoreImage做图片过滤

        extension UIImage{
            var disabled: UIImage?{
                let ciImage = CIImage(image: self)
                let grayscale = ciImage?.applyingFilter("CIColorControls",
                                                        parameters: [ kCIInputSaturationKey: 0.0 ])
                if let gray = grayscale{
                    return UIImage(ciImage: gray)
                }
                else{
                    return nil
                }
            }
        }
        

        当然,在 Swift 5 中

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-22
          相关资源
          最近更新 更多