How do you change UIButton image alpha on disabled state?

后端 未结 11 2196
庸人自扰
庸人自扰 2021-02-03 23:22

I have a UIButton with an image and on its disabled state, this image should have .3 alpha.

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIIm         


        
11条回答
  •  借酒劲吻你
    2021-02-04 00:06

    In case anyone needs it, here's an answer in Swift:

    Subclass UIButton and override enabled

    class MyCustomButton: UIButton {
    
    override var enabled: Bool {
        didSet{
            alpha = enabled ? 1.0 : 0.3
        }
    }
    

    Set the class of any button you want to have this property to "MyCustomButton" (or whatever you choose to name it)

     @IBOutlet weak var nextButton: MyCustomButton!
    

提交回复
热议问题