I created a button on my view controller which has a predefined background image. I created an action and an outlet for this button. I want when the user taps the button to change the background image of this button. How can i do that? I tried to put into the action method of the button something like this:
snapshotCheckbox.image = [UIImage imageNamed:@"snapshot.png"];
but i guess this method is for UImageViews. How can i do the same thing for a button? Thank you very much for reading my post :D
you can set the image for a given state of the button in the viewDidLoad:
[myButton setBackgroundImage:[UIImage imageNamed:@"myBackgroundImage.png"] forState:UIControlStateHighlighted];
A button has two properties
image
and backgroundImage
..
For setting image
use
button.currentImage = image (or)
[button setImage:image ForState:UIControlStateNormal];
For setting backgroundImage
use
button.currentBackgroundImage = image (or)
[button setBackgroundImage:image ForState:UIControlStateNormal];
First set the tybe of the button to
button = [UIButton buttonWithType :UIButtonTypeCustom];
then use
[button setImage:[UIImage imageNamed:@"imagename.type"] 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