How do you change UIButton image alpha on disabled state?

后端 未结 11 2240
庸人自扰
庸人自扰 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:22

    Swift 5:

    Here's a solution that extends the UIButton class. No need to sub-class. In my example, button alpha is set to 0.55 if .isEnabled is false, and 1.0 if it's true.

    extension UIButton {

    override open var isEnabled: Bool{
        didSet {
            self.alpha = isEnabled ? 1.0 : 0.55
        }
    }
    

    }

提交回复
热议问题