UIButton with an image losing its new image

I have a UIButton (custom, with image) configured in IB, which is the IBOutlet in my code.

In my viewDidLoad method in viewController I am trying to modify an existing UIButton image

UIImage *newbuttonimage = [UIImage imageNamed:@"newbuttonimage.png"];
testbutton.imageView.image = newbuttonimage;

      

OK, this works when the app starts, but whenever you interact with the button (press it) it changes to the original image (set to IB). There is no other code to change images throughout the project, so what's going on?

By the way, when I put the above code in any other part of my project, it doesn't change the image.

+2


source to share


2 answers


You should try to use setImage:forState

images instead of direct destination; there are several UIButton states and, if not set properly, can give unwanted behavior (similar to the ones you see).



+9


source


Doh! I had to use this code instead:



UIImage *newbuttonimage = [UIImage imageNamed:@"newbuttonimage.png"];
[testbutton setImage:newbuttonimage forState:UIControlStateNormal];

      

0


source







All Articles