UIButton's imageView property and hidden/alpha value
Asked Answered
S

5

6

I have a UIButton that should display an activity indicator instead of an image in some situations. What I do at the moment is setting the hidden property of the button's imageView to YES and back. I also tried this with setting the alpha value to 0.0f and back to 1.0f.

This works until the state of the button changes. This resets the properties of the imageView and leads to hidden == NO and alpha == 1.0f.

Has anybody did something similar or has an idea how to hide the imageView of a button while the rest of it stays visible?

Speaking answered 26/7, 2012 at 16:7 Comment(0)
P
7

It is simple. Button have different states. Just set zero image for selected state and your image for normal state. When you want to display activity indicator set button state as selected and add indicator as button subview.

- (void)initializeButton {
    button = [[UIButton alloc] init];
    [button addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];

    [button setImage:[[UIImage alloc] init] forState:UIControlStateSelected]; //zero image
    [button setImage:yourImage forState:UIControlStateSelected]; //your image
}

- (void)buttonDidClick:(UIButton *)button {
    button.selected = YES;
    /* Add yor activity indicator with some view tag or save reference to it */
}

Call next method when your activity is finished

- (void)finishActivityWithButton:(UIButton *)button {
    button.selected = NO;
    /* Remove your activity indicator from button */
}
Polemist answered 10/4, 2014 at 11:24 Comment(0)
J
27

You can achieve this by playing with the transform property of view's layer i.e

To hide

swift code

button.imageView?.layer.transform = CATransform3DMakeScale(0.0, 0.0, 0.0)

objective-C code

button.imageView.layer.transform = CATransform3DMakeScale(0, 0, 0);

To unhide

button.imageView?.layer.transform = CATransform3DIdentity

objective-C code

button.imageView.layer.transform = CATransform3DIdentity
Joellejoellen answered 11/12, 2016 at 21:4 Comment(0)
P
7

It is simple. Button have different states. Just set zero image for selected state and your image for normal state. When you want to display activity indicator set button state as selected and add indicator as button subview.

- (void)initializeButton {
    button = [[UIButton alloc] init];
    [button addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];

    [button setImage:[[UIImage alloc] init] forState:UIControlStateSelected]; //zero image
    [button setImage:yourImage forState:UIControlStateSelected]; //your image
}

- (void)buttonDidClick:(UIButton *)button {
    button.selected = YES;
    /* Add yor activity indicator with some view tag or save reference to it */
}

Call next method when your activity is finished

- (void)finishActivityWithButton:(UIButton *)button {
    button.selected = NO;
    /* Remove your activity indicator from button */
}
Polemist answered 10/4, 2014 at 11:24 Comment(0)
U
3

set the tint color to [UIColor clearColor] if you are using a template image (should maybe work on others too, didn't try)

[[button imageView] setTintColor:[UIColor clearColor]];
Unbending answered 9/2, 2015 at 22:1 Comment(0)
I
2

You can achieve this by removing the image from the button when you want to hide it and assigning the image back to button when you need to show the image.

When you want to hide the image write like:

[yourButton setImage:nil forState:UIControlStateNormal];

and when you want to show the image back:

[yourButton setImage:yourImage forState:UIControlStateNormal];

Here yourButton is your UIButton and yourImage is a UIImage which holds the button image.

Inscription answered 26/7, 2012 at 17:15 Comment(0)
H
0

change the transform or select state will cause another problem, the best way is using category:

const char kNormalImage;
const char kSelectImage;
const char kDisableImage;

@interface UIButton ()

@property (nonatomic, strong) UIImage *normalImage;
@property (nonatomic, strong) UIImage *selectImage;
@property (nonatomic, strong) UIImage *disableImage;

@end

@implementation UIButton (Hidden)

- (void)setImageViewHidden:(BOOL)hidden {
    if (hidden) {
        self.normalImage = [self imageForState:UIControlStateNormal];
        self.selectImage = [self imageForState:UIControlStateSelected];
        self.disableImage = [self imageForState:UIControlStateDisabled];

        UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, NO, 0.0);
        UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        [self setImage:blank forState:UIControlStateNormal];
        [self setImage:blank forState:UIControlStateSelected];
        [self setImage:blank forState:UIControlStateDisabled];
    } else {
        [self setImage:self.normalImage forState:UIControlStateNormal];
        [self setImage:self.selectImage forState:UIControlStateSelected];
        [self setImage:self.disableImage forState:UIControlStateDisabled];
    }
}

- (UIImage *)normalImage {
    return objc_getAssociatedObject(self, &kNormalImage);
}

- (void)setNormalImage:(UIImage *)normalImage {
    objc_setAssociatedObject(self, &kNormalImage, normalImage,  OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIImage *)selectImage {
    return objc_getAssociatedObject(self, &kSelectImage);
}

- (void)setSelectImage:(UIImage *)selectImage {
    objc_setAssociatedObject(self, &kSelectImage, selectImage,  OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIImage *)disableImage {
    return objc_getAssociatedObject(self, &kDisableImage);
}

- (void)setDisableImage:(UIImage *)disableImage {
    objc_setAssociatedObject(self, &kDisableImage, disableImage,    OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
Horrendous answered 20/5, 2020 at 7:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.