【问题标题】:Change the color of the UIButton when its state is .disabled状态为 .disabled 时更改 UIButton 的颜色
【发布时间】:2018-09-06 03:12:46
【问题描述】:

我正在尝试更改禁用时的按钮图像颜色。提一下我的按钮可能也启用了

问题是它总是灰色的。

    sendCodeBtn.isEnabled = false
    sendCodeBtn.setImage(#imageLiteral(resourceName: "validated_phone").withRenderingMode(.alwaysTemplate), for: .disabled)
    sendCodeBtn.tintColor = Theme.defaultColor()

【问题讨论】:

    标签: ios swift swift3 uibutton


    【解决方案1】:

    要仅通过更改 isEnabled 属性值来更改按钮的颜色,您可以像这样添加观察者:

    btnConfirm.addObserver(self, forKeyPath: #keyPath(UIButton.isEnabled), options: .new, context: nil)
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        btnConfirm.backgroundColor = btnConfirm.isEnabled ? .red : .lightGray
    }
    

    然后只改变 isEnabled 属性来改变背景颜色。

    【讨论】:

      【解决方案2】:
      @IBAction btn : UIButton!
      if btn.state == .isSelected{
          btn.backgroundColor = UIColor.red
      }
      

      【讨论】:

        【解决方案3】:

        swift 中,您可以通过多种方式做到这一点 -

        @IBAction func onClickButtonStateChanged(sender: UIButton) {
            if(sender.isSelected){
                sender.backgroundColor = UIColor.red  //Highlighted
            }else{
                sender.backgroundColor = UIColor.grey . // disabled
            }
        }
        

        或者

        override var isHighlighted: Bool {
            didSet {
                backgroundColor = isHighlighted ? UIColor.red : UIColor.grey  // selected : disabled 
            }
        }
        

        另一种方式-

           button.setBackgroundImage(UIImage(named: "button_image"), for: .normal)
           button.setBackgroundImage(UIImage(named: "button_image_selected"), for: .selected)
           button.setBackgroundImage(UIImage(named: "button_image_selected"), for: .disabled)
           button.setBackgroundImage(UIImage(named: "button_image_selected"), for: [.selected, .highlighted])
        

        【讨论】:

          【解决方案4】:

          您可以在 故事板 中更改 UIButton 的所有属性。

          如图所示改变状态:

          然后设置颜色和图像等属性。

          以编程方式

          有图片:

          sendCodeBtn.setBackgroundImage(UIImage(named: "yourImage.png"), for: .disabled) 
          

          带背景色:

          sendCodeBtn.backgroundColor = UIColor .red
          

          【讨论】:

          • 问题是自从第一次启用以来我更喜欢以编程方式进行操作
          【解决方案5】:

          斯威夫特 4

          如果您想以编程方式执行此操作(看起来不漂亮,但可能):

          func changeDisableButtonColor(button: UIButton, color: UIColor) {
              let rect = CGRect(x: 0.0, y: 0.0, width: myButton.frame.width, height: myButton.frame.height)
              UIGraphicsBeginImageContext(rect.size)
          
              let context = UIGraphicsGetCurrentContext()
              context?.setFillColor(color.cgColor)
              context?.fill(rect)
          
              let image: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
              UIGraphicsEndImageContext()
          
              button.setBackgroundImage(image, for: .disabled)
          }
          

          用法:

          changeDisableButtonColor(button: myButton, color: .red)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-09-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多