How do you change UIButton image alpha on disabled state?

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

    Swift 3:

    extension UIImage {
        func copy(alpha: CGFloat) -> UIImage? {
            UIGraphicsBeginImageContextWithOptions(size, false, scale)
            draw(at: CGPoint.zero, blendMode: .normal, alpha: alpha)
            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return newImage
        }
    }
    

    Usage:

    button.setImage(image.copy(alpha: 0.3), for: .disabled)
    

提交回复
热议问题