Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change background image on a button , iOS?

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

like image 369
user1511244 Avatar asked Jul 13 '12 11:07

user1511244


3 Answers

you can set the image for a given state of the button in the viewDidLoad:

[myButton setBackgroundImage:[UIImage imageNamed:@"myBackgroundImage.png"] forState:UIControlStateHighlighted];
like image 88
Joe Avatar answered Nov 13 '22 02:11

Joe


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];
like image 33
Mr.Anonymous Avatar answered Nov 13 '22 01:11

Mr.Anonymous


First set the tybe of the button to

button = [UIButton buttonWithType :UIButtonTypeCustom];

then use

 [button setImage:[UIImage imageNamed:@"imagename.type"]   ForState:UIControlStateNormal];
like image 1
Mutawe Avatar answered Nov 13 '22 00:11

Mutawe