UIButton setImage not working

I'm losing my mind over this one. I have UIButton connected as Outlet. I want to change it's image. In the viewDidLoad function, I try to set the image like so:

[button1 setImage:[UIImage imageNamed:@"house.png"] forState:UIControlStateNormal];

Nothing happens. However, if I use ImagePicker, it somehow works. In the ImagePickers function "didFinishPickingImage", I repeat the exactly same command as above for setting image and it works, the image is displayed normally.

I've been thinking that maybe the button's image is not refreshing itself, but trying to call [button1 setNeedsDisplay], or [button1.imageView setNeedsDisplay] or even [button1.imageView.image setNeedsDisplay] does nothing.

Does anyone have any idea about this? I've wasted two hours by now and I'm really getting frustrated, because I'm certain that the reason must be so stupid it's unbelivable.


Please check your button type.

If your button type is "System" you should change it to "Custom"

Two things to do here:

#1, change your code to:

[button1 setImage:[UIImage imageNamed:@"house.png"] forState:UIControlStateNormal];

and #2

make sure your UIImage returns a valid image.

In other words,

UIImage * imageToSet = [UIImage imageNamed: @"house.png"];
    [button1 setImage:[UIImage imageNamed:@"house.png"] forState:UIControlStateNormal];
} else {
    // if you see this line in your console, then you know where to look for a problem
    NSLog( @"why is my image object nil?");

For anyone who will find themselves in a similar situation; I was using the mentioned button inside a custom cell for table view. However, the cells aren't loaded right away (i.e. inside viewDidLoad/viewWillAppear etc.), but inside the tableView:cellForRowAtIndexPath function. All my button variables were nil at that point. My solution was to load the image after the cell has been created and it has worked straight away afterwards.

Here are a couple additional notes. UIButton setImage: forState: is the correct method:

[button1 setImage:[UIImage imageNamed:@"house.png"] forState:UIControlStateNormal];

imageNamed is a great convenience method for grabbing the image, but make sure it returns an object. Forgetting to include the image file in the current target gets me all of the time.

[UIImage imageNamed:@"house.png"] 

You also don't need the extension. The SDK works this out.

[UIImage imageNamed:@"house"] 

And even though UIControlState is a bit mask, you cannot OR the values together. This does not not not work and I must must must remember this because it gets me every time I use the call.

[button1 setImage:[UIImage imageNamed:@"house"] forState:UIControlStateNormal|UIControlStateSelected]; 

// YES
[button1 setImage:[UIImage imageNamed:@"house"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:@"house"] forState:UIControlStateSelected]; 

Was getting the same problem when UIbutton was in UITableViewCell.

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"detail.png"] forState:UIControlStateNormal];

should work in UITableViewCell

For me it was to move the line:

[button1 setImage:[UIImage imageNamed:@"house.png"] forState:UIControlStateNormal];

from awakeFromNib method

to viewDidLoad method

I guess, don't create like this UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; by this create method ,UIButton is create by use default type,so your background image is not work. try this, UIButton *nextButton = [[UIButton alloc] initWithFrame:CGRectMake(9, 154, 300, 48)]; or other,except buttonWithType

Last thing you'll want to check - make sure you're setting the image on the main thread! Just ran into an issue where I was setting a button image in a callback from ALAssetLibrary (fetching images from photo library). The image wouldn't update until I tapped the button. Turns out the callback was happening off main...

This is happening because the state of the Button doesn't stays selected once clicked by default. I solved it by making an IBaction on that button click and toggling the selected state. -(IBAction) checkButtonClicked:(id)sender{ checkButton.selected = !checkButton.selected; }

In my case, it is drawn by custom animation code and not set by the image, so it doesn't work by setting any image, it has to change the animation code.

Use setBackgroundImage:forState:

Need Your Help

How do I output lists as a table in Jupyter notebook?

python jupyter-notebook

I know that I've seen some example somewhere before but for the life of me I cannot find it when googling around.

How to change the tint color of the clear button on a UITextField

ios objective-c swift uibutton uitextfield

I have an auto-generated clear button on my UITextfield, with the default blue tint color. I cannot change the tint color to white. I have tried modifying the storyboard and code without success, a...