Set UIButton Image and Maintain Transparency

I have a UIButton (set to input "Custom"). If I set the image in Interface Builder, the image will be displayed correctly, with transparency. But if I try to install it programmatically while the application is running (for example, to change the image displayed on the button), the transparency does not display correctly - instead, I get a white background for the image.

This is how I set the button image in the code:

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

It would be extremely grateful for any ideas.

+4
source share
2 answers

Xcode remembered the previous version of the images, without transparency - although I added new versions with transparency. I had to make a product - Cleanse and now everything is as expected.

+1
source

The method used puts the image in 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 too ... :)

0
source

Source: https://habr.com/ru/post/1339692/


All Articles