Set UIButton image and maintain transparency
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.
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.
The method you are using puts the image on the foreground of the button. I used the following method and it worked
[die1Button setBackgroundImage:[UIImage imageNamed:@"yourImageName.png"] forState:UIControlStateNormal];
Hope this works for you as well... :)
精彩评论