How to change Custom Button's Image when Tapping in iOS?
Solution 1
assuming this is a standard UIButton, you can do the following when initializing the button:
[btnObj setImage:[UIImage imageNamed:@"imgName.png"] forState:UIControlStateHighlighted];
or for the Background image:
[btnObj setBackgroundImage:[UIImage imageNamed:@"imgName.png"] forState:UIControlStateHighlighted];
Solution 2
First create a button as a member variable of your view controller. Let's call it UIButton* myButton
.
Then initialize the button like so, and set its image to the image you want it to display originally:
myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[myButton setImage:[UIImage imageNamed:@"OriginalImage"] forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(onClickMyButton) forControlEvents:UIControlEventTouchUpInside];
The third line there will make it so that when the button is clicked, the function "onClickMyButton" is called. So then you write this function, which will change the image to 'NewImage':
-(void) onClickMyButton
{
[myButton setImage:[UIImage imageNamed:@"NewImage"] forState:UIControlStateNormal];
}
And voila! Problem solved :)
Solution 3
Below two methods you can use – setBackgroundImage:forState: – setImage:forState: check the UIButton

Fire Fist
Updated on June 14, 2022Comments
-
Fire Fist 11 months
I used custom button with Image. In normally , When we tapping button , the color of button has been changed.
When i tapping that custom button , i want to change image from custom button.
How can i change it?
Thanks.