6

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

user1511244
  • 725
  • 3
  • 11
  • 15

7 Answers7

14

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

[myButton setBackgroundImage:[UIImage imageNamed:@"myBackgroundImage.png"] forState:UIControlStateHighlighted];
Joe
  • 2,987
  • 15
  • 24
  • Will this take care of resolution also ? for example will it load myBackgroundImage@2x.png based on the resolution? – Yugandhar Pathi Feb 04 '14 at 06:29
  • 1
    UIImage imageNamed documentation: On a device running iOS 4 or later, the behavior is identical if the device’s screen has a scale of 1.0. If the screen has a scale of 2.0, this method first searches for an image file with the same filename with an @2x suffix appended to it. For example, if the file’s name is button, it first searches for button@2x. If it finds a 2x, it loads that image and sets the scale property of the returned UIImage object to 2.0. Otherwise, it loads the unmodified filename and sets the scale property to 1.0. – Joe Feb 13 '14 at 17:49
3

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];
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Mr.Anonymous
  • 820
  • 2
  • 13
  • 22
1

First set the tybe of the button to

button = [UIButton buttonWithType :UIButtonTypeCustom];

then use

 [button setImage:[UIImage imageNamed:@"imagename.type"]   ForState:UIControlStateNormal];
Mutawe
  • 6,464
  • 3
  • 47
  • 90
0

One solution to do this would be to display the image in a UIImageView and put a transparent UIButton on top of the UIImageView. In Interface Builder you can change a UIButton to "custom". This would allow you to change the image displayed in the UIImageView easily when handling the action triggered when the UIButton is pushed.

Hope this helps.

werner
  • 859
  • 4
  • 8
0

You have the setBackgroundImage:forState: method on the button object. See Setting an image for a UIButton in code for more information (seconds answer).

Also, UIButtons automatically change the image when pressed if you set an image for the UIControlStateHighlighted state (though only as long as the user keeps pressing on the button).

Community
  • 1
  • 1
0

Setbutton

if u need image while clicking

[snapshotCheckbox setImage:[UIImage imageNamed:@"snapshot.png"]  forState:UIControlStateHighlighted];

or if u want image after Selecting it

[snapshotCheckbox setImage:[UIImage imageNamed:@"snapshot.png"] forState:UIControlStateSelected];

and on that onclick function mention as

snapshotCheckbox.Selected=YES;
Novarg
  • 7,390
  • 3
  • 38
  • 74
jothikenpachi
  • 656
  • 6
  • 12
0

To set an image on a button, just press the button you want an image to in Main.storyboard, then, in the utilities bar to the right, press the attributes inspector and set the background to the image you want! Make sure you have the picture you want in the supporting files to the left.

benka
  • 4,732
  • 35
  • 47
  • 58
JELLYFUN
  • 39
  • 1