I want to change the button image on buttons click event. Here is what i am trying.
-(IBAction)editObjectImage:(id)sender
{
if (editButtonState == NO)
{
[editButton setImage:nil forState:UIControlStateNormal];
[editButton setImage:[UIImage imageNamed:@"done2.png"] forState:UIControlStateNormal];
}
else
{
[editButton setImage:nil forState:UIControlStateNormal];
[editButton setImage:[UIImage imageNamed:@"edit.png"] forState:UIControlStateNormal];
}
}
But my Button image is not changing. What's wrong with code?
The image buttons in the HTML document can be created by using the type attribute of an <input> element. Image buttons also perform the same function as submit buttons, but the only difference between them is that you can keep the image of your choice as a button.
The default button in HTML can be changed to an image using CSS. The required button is selected using the respective CSS selector. The background property can then be set to include a background image and change the image type as required. The border of the button can also be removed to show only the image itself.
Using storyboard, select the button, then in the size inspect click the dropdown on size just above Content inset. There is a list of sizes to select from, and this will adjust your image size(if you use system image). The default size is already set when you added the button on the storyboard to the View Controller.
I think you aren't getting round to changing editButtonState
. Your code can be reduced to.
-(IBAction)editObjectImage:(id)sender
{
UIButton *theButton = (UIButton*)sender;
if (editButtonState == NO) {
[theButton setImage:[UIImage imageNamed:@"done2.png"] forState:UIControlStateNormal];
} else {
[theButton setImage:[UIImage imageNamed:@"edit.png"] forState:UIControlStateNormal];
}
editButtonState = !editButtonState;
}
This is my working code:
NSString *shoppingListButtonImageName = @"notepad-selected";
UIImage *slImage = [UIImage imageNamed:shoppingListButtonImageName];
//put a breakpoint here to check that slImage is not nil.
[self.shoppingListButton setImage:slImage forState:UIControlStateNormal];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With