How do you change UIButton image alpha on disabled state?

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

    Subclassing UIButton and extending the setEnabled: method seems to work:

    - (void) setEnabled:(BOOL)enabled {    
        [super setEnabled:enabled];
        if (enabled) {
            self.imageView.alpha = 1;
        } else {
            self.imageView.alpha = .25;
        }
    }
    

    I haven't tested it thoroughly, but did find the value resets if the screen is rotated. Adding the same code to the setFrame: method fixes that, but I'm not sure if there are other situations where this value is changed.

提交回复
热议问题