Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton's imageView property and hidden/alpha value

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?

like image 854
Michael Ochs Avatar asked Jul 26 '12 16:07

Michael Ochs


People also ask

How to hide UIButton image in swift?

When you want to hide the image write like: [yourButton setImage:nil forState:UIControlStateNormal]; [yourButton setImage:yourImage forState:UIControlStateNormal]; Here yourButton is your UIButton and yourImage is a UIImage which holds the button image.

What is a UIButton?

A control that executes your custom code in response to user interactions.

What is button in ios swift?

It is a control that enables the user to interact with the application. It is used to trigger the events performed by the user. It executes the custom code in response to user interactions. class UIButton : UIControl.


3 Answers

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
like image 157
IronMan Avatar answered Oct 23 '22 17:10

IronMan


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 */
}
like image 20
Denis Avatar answered Oct 23 '22 15:10

Denis


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]];
like image 3
Peter Lapisu Avatar answered Oct 23 '22 15:10

Peter Lapisu