Xcode – Set UIButton image and maintain transparency

imagetransparencyuibuttonxcode

I have a UIButton (set to type "Custom"). If I set the image in Interface Builder, the image is shown properly, with transparency. But if I try to set it programmatically while the App is running (for example, to change the image displayed on the button), the transparency isn't displayed correctly – instead I get a white background to the image.

This is how I'm setting the button's image in code:

[die1Button setImage:[UIImage imageNamed:die1.dieImage] forState:UIControlStateNormal];

Would hugely appreciate any ideas.

Best Answer

Xcode was remembering the previous version of the images, the ones without transparency - even though I had added the new versions with transparency. I had to do a Product - Clean and now it's all as expected.