Ios – UIButton’s imageView property and hidden/alpha value

alphahiddeniosuibuttonuikit

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?

Best Answer

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
Related Topic