如何在禁用状态下更改UIButton图像alpha?

时间:2022-09-04 09:37:10

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

我有一个带有图像的UIButton,并且在禁用状态下,此图像应该具有.3 alpha。

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *arrowImage = [UIImage imageNamed:@"arrow.png"];
[button setImage:arrowImage forState:UIControlStateNormal];

// The arrow should be half transparent here
[button setImage:arrowImage forState:UIControlStateDisabled];

How do I accomplish this?

我该如何做到这一点?

UPDATE: I noticed, by default UIButton does reduce the alpha of the image on disabled state (probably at .5?). But I'd like to know how to fine-tune this value.

更新:我注意到,默认情况下,UIButton会在禁用状态下降低图像的alpha值(可能是.5?)。但我想知道如何微调这个值。

9 个解决方案

#1


16  

If setting alpha while the button is disabled doesn't work, then just make your disabled image at the alpha value you desire.

如果在禁用按钮时设置alpha不起作用,则只需将您的禁用图像设置为所需的alpha值。

Just tested this, you can set the alpha on the UIButton, regardless of state and it works just fine.

刚试过这个,你可以在UIButton上设置alpha,无论状态如何,它都可以正常工作。

self.yourButton.alpha = 0.25;

#2


13  

Credit goes to @bryanmac for his helpful answer. I used his code as a starting point, but found the same thing can be achieved without using a UIImageView.

感谢@bryanmac的回答。我使用他的代码作为起点,但发现可以在不使用UIImageView的情况下实现相同的功能。

Here's my solution:

这是我的解决方案:

- (UIImage *)translucentImageFromImage:(UIImage *)image withAlpha:(CGFloat)alpha
{
    CGRect rect = CGRectZero;
    rect.size = image.size;

    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:rect blendMode:kCGBlendModeScreen alpha:alpha];
    UIImage * translucentImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return translucentImage;
}

Set the button's background image for disabled state:

将按钮的背景图像设置为禁用状态:

UIImage * disabledBgImage = [self translucentImageFromImage:originalBgImage withAlpha:0.5f];
[button setBackgroundImage:disabledBgImage forState:UIControlStateDisabled];

EDIT:

编辑:

I refined my solution further by creating a category on UIImage with this method:

我通过使用此方法在UIImage上创建一个类别来进一步改进我的解决方案:

- (UIImage *)translucentImageWithAlpha:(CGFloat)alpha
{
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
    CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
    [self drawInRect:bounds blendMode:kCGBlendModeScreen alpha:alpha];

    UIImage * translucentImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return translucentImage;
}

Set the button's background image for disabled state:

将按钮的背景图像设置为禁用状态:

UIImage * disabledBgImage = [originalBgImage translucentImageWithAlpha:0.5f];
[button setBackgroundImage:disabledBgImage forState:UIControlStateDisabled];

#3


7  

You need two instances of UIImage, one for enabled and one for disabled.

您需要两个UIImage实例,一个用于启用,一个用于禁用。

The tough part is for the disabled one, you can't set alpha on UIImage. You need to set it on UIImageView but button doesn't take an UIImageView, it takes a UIImage.

困难的部分是为残疾人,你不能在UIImage上设置alpha。你需要在UIImageView上设置它,但是按钮不需要UIImageView,它需要一个UIImage。

If you really want to do this, you can load the same image into the disabled button state after creating a resultant image from the UIImageView that has the alpha set on it.

如果您确实想这样做,可以在从其上设置了alpha集的UIImageView创建结果图像后,将相同的图像加载到禁用的按钮状态。

UIImageView *uiv = [UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"];

// get resultant UIImage from UIImageView
UIGraphicsBeginImageContext(uiv.image.size);
CGRect rect = CGRectMake(0, 0, uiv.image.size.width, uiv.image.size.height);
[uiv.image drawInRect:rect blendMode:kCGBlendModeScreen alpha:0.2];  

UIImage *disabledArrow = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext();

[button setImage:disabledArrow forState:UIControlStateDisabled];

That's a lot to go through to get an alpha controlled button image. There might be an easier way but that's all I could find. Hope that helps.

获得alpha控制的按钮图像需要经过很多工作。可能有一种更简单的方法,但这就是我能找到的。希望有所帮助。

#4


4  

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

子类化UIButton并扩展setEnabled:方法似乎工作:

- (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.

我没有彻底测试过,但是如果屏幕旋转,确实发现值重置了。将相同的代码添加到setFrame:方法可以解决这个问题,但是我不确定是否存在其他更改此值的情况。

#5


3  

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

如果有人需要它,这里是Swift的答案:

Subclass UIButton and override enabled

子类UIButton和覆盖已启用

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)

将您想要拥有此属性的任何按钮的类设置为“MyCustomButton”(或您选择的任何名称)

 @IBOutlet weak var nextButton: MyCustomButton!

#6


2  

The button’s image view. (read-only)

按钮的图像视图。 (只读)

@property(nonatomic, readonly, retain) UIImageView *imageView

Although this property is read-only, its own properties are read/write.

虽然此属性是只读的,但它自己的属性是可读/写的。

#7


1  

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:

我发现这些解决方案都不起作用。最干净的方法是子类化,而不是使用self.imageView,只需添加自己的自定义imageView,如下所示:

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

Then add your custom alpha like so:

然后像这样添加自定义alpha:

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

#8


1  

Swift 3:

斯威夫特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)

#9


0  

- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image; }

Use: [btnRegister setBackgroundImage:[self imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateDisabled]; btnRegister.enabled = false;

使用:[btnRegister setBackgroundImage:[self imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateDisabled]; btnRegister.enabled = false;

#1


16  

If setting alpha while the button is disabled doesn't work, then just make your disabled image at the alpha value you desire.

如果在禁用按钮时设置alpha不起作用,则只需将您的禁用图像设置为所需的alpha值。

Just tested this, you can set the alpha on the UIButton, regardless of state and it works just fine.

刚试过这个,你可以在UIButton上设置alpha,无论状态如何,它都可以正常工作。

self.yourButton.alpha = 0.25;

#2


13  

Credit goes to @bryanmac for his helpful answer. I used his code as a starting point, but found the same thing can be achieved without using a UIImageView.

感谢@bryanmac的回答。我使用他的代码作为起点,但发现可以在不使用UIImageView的情况下实现相同的功能。

Here's my solution:

这是我的解决方案:

- (UIImage *)translucentImageFromImage:(UIImage *)image withAlpha:(CGFloat)alpha
{
    CGRect rect = CGRectZero;
    rect.size = image.size;

    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:rect blendMode:kCGBlendModeScreen alpha:alpha];
    UIImage * translucentImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return translucentImage;
}

Set the button's background image for disabled state:

将按钮的背景图像设置为禁用状态:

UIImage * disabledBgImage = [self translucentImageFromImage:originalBgImage withAlpha:0.5f];
[button setBackgroundImage:disabledBgImage forState:UIControlStateDisabled];

EDIT:

编辑:

I refined my solution further by creating a category on UIImage with this method:

我通过使用此方法在UIImage上创建一个类别来进一步改进我的解决方案:

- (UIImage *)translucentImageWithAlpha:(CGFloat)alpha
{
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
    CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
    [self drawInRect:bounds blendMode:kCGBlendModeScreen alpha:alpha];

    UIImage * translucentImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return translucentImage;
}

Set the button's background image for disabled state:

将按钮的背景图像设置为禁用状态:

UIImage * disabledBgImage = [originalBgImage translucentImageWithAlpha:0.5f];
[button setBackgroundImage:disabledBgImage forState:UIControlStateDisabled];

#3


7  

You need two instances of UIImage, one for enabled and one for disabled.

您需要两个UIImage实例,一个用于启用,一个用于禁用。

The tough part is for the disabled one, you can't set alpha on UIImage. You need to set it on UIImageView but button doesn't take an UIImageView, it takes a UIImage.

困难的部分是为残疾人,你不能在UIImage上设置alpha。你需要在UIImageView上设置它,但是按钮不需要UIImageView,它需要一个UIImage。

If you really want to do this, you can load the same image into the disabled button state after creating a resultant image from the UIImageView that has the alpha set on it.

如果您确实想这样做,可以在从其上设置了alpha集的UIImageView创建结果图像后,将相同的图像加载到禁用的按钮状态。

UIImageView *uiv = [UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"];

// get resultant UIImage from UIImageView
UIGraphicsBeginImageContext(uiv.image.size);
CGRect rect = CGRectMake(0, 0, uiv.image.size.width, uiv.image.size.height);
[uiv.image drawInRect:rect blendMode:kCGBlendModeScreen alpha:0.2];  

UIImage *disabledArrow = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext();

[button setImage:disabledArrow forState:UIControlStateDisabled];

That's a lot to go through to get an alpha controlled button image. There might be an easier way but that's all I could find. Hope that helps.

获得alpha控制的按钮图像需要经过很多工作。可能有一种更简单的方法,但这就是我能找到的。希望有所帮助。

#4


4  

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

子类化UIButton并扩展setEnabled:方法似乎工作:

- (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.

我没有彻底测试过,但是如果屏幕旋转,确实发现值重置了。将相同的代码添加到setFrame:方法可以解决这个问题,但是我不确定是否存在其他更改此值的情况。

#5


3  

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

如果有人需要它,这里是Swift的答案:

Subclass UIButton and override enabled

子类UIButton和覆盖已启用

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)

将您想要拥有此属性的任何按钮的类设置为“MyCustomButton”(或您选择的任何名称)

 @IBOutlet weak var nextButton: MyCustomButton!

#6


2  

The button’s image view. (read-only)

按钮的图像视图。 (只读)

@property(nonatomic, readonly, retain) UIImageView *imageView

Although this property is read-only, its own properties are read/write.

虽然此属性是只读的,但它自己的属性是可读/写的。

#7


1  

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:

我发现这些解决方案都不起作用。最干净的方法是子类化,而不是使用self.imageView,只需添加自己的自定义imageView,如下所示:

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

Then add your custom alpha like so:

然后像这样添加自定义alpha:

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

#8


1  

Swift 3:

斯威夫特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)

#9


0  

- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image; }

Use: [btnRegister setBackgroundImage:[self imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateDisabled]; btnRegister.enabled = false;

使用:[btnRegister setBackgroundImage:[self imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateDisabled]; btnRegister.enabled = false;