Anas Tasneem
Anas Tasneem

Reputation: 1

Objective C How can we change the background image of a button in just when it is clicked and then go back to normal?

[myButton setImage:[UIImage imageNamed:@"btn_normal"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:@"btn_highlighted"] forState:UIControlStateHighlighted];
[myButton setImage:[UIImage imageNamed:@"btn_highlighted"] forState:UIControlStateSelected];
myButton.showsTouchWhenHighlighted = YES;

I have tried this code and it is not working.

Upvotes: 0

Views: 111

Answers (2)

Bansi Hirpara
Bansi Hirpara

Reputation: 135

Implement onTouchDown action of that button and set button background image in that function by [btn setBackgroundImage: forState:]

Upvotes: 0

Lukas Würzburger
Lukas Würzburger

Reputation: 6673

You should use the setBackgroundImage:forState. The setImage:forState changes the image which is on the same depth level as the button title. Additionally the backgroundImage is automatically fit to the button's view bounds. The image is not.

Upvotes: 2

Related Questions