How to change background image on button, iOS?

I created a button on my view controller that has a predefined background image. I created an action and an exit for this button. I want the user to click a button to change the background image of this button. How can i do this? I tried applying the button action method something like this:

snapshotCheckbox.image = [UIImage imageNamed:@"snapshot.png"];

but I assume this method is for UImageViews. How can I do the same for a button? Thanks so much for reading my post: D

+5
source share
7 answers

UIImageView UIButton UIImageView. Interface Builder UIButton "custom". , UIImageView, , UIButton.

, .

0

viewDidLoad:

[myButton setBackgroundImage:[UIImage imageNamed:@"myBackgroundImage.png"] forState:UIControlStateHighlighted];
+13

properties image backgroundImage..

setting image

button.currentImage = image (or) 
[button setImage:image   ForState:UIControlStateNormal];

setting backgroundImage

button.currentBackgroundImage = image (or) 
[button setBackgroundImage:image  ForState:UIControlStateNormal];
+3

button = [UIButton buttonWithType :UIButtonTypeCustom];

 [button setImage:[UIImage imageNamed:@"imagename.type"]   ForState:UIControlStateNormal];
+1

setBackgroundImage:forState: . . UIButton ( ).

, UIButtons , UIControlStateHighlighted ( , ).

0

Setbutton

, u

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

u

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

, onclick

snapshotCheckbox.Selected=YES;
0

, , , Main.storyboard, and set the background for the image you want! Make sure you have the image you want in the supporting files on the left.

0
source

All Articles