How do you change UIButton image alpha on disabled state?

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

    I found that none of these solutions really work. The cleanest way of doing it is to subclass, and instead of using self.imageView, just add your own custom imageView like this:

    _customImageView = [[UIImageview alloc] initWithImage:image];
    

    Then add your custom alpha like so:

    - (void)setEnabled:(BOOL)enabled {
        [super setEnabled:enabled];
        if (enabled) {
            _customImageView.alpha = 1.0f;
        } else {
            _customImageView.alpha = 0.25f;
        }
    }
    

提交回复
热议问题