Reputation: 7050
I used custom button with Image. In normally , When we tapping button , the color of button has been changed.
When i tapping that custom button , i want to change image from custom button.
How can i change it?
Thanks.
Upvotes: 4
Views: 13565
Reputation: 7324
Jose Rafael Santiago Jr.'s Answer in Swift 3:
yourButton.setImage(UIImage(named: "imgName"), for: .normal)
or for the background image:
yourButton.setBackgroundImage(UIImage(named: "imgName"), for: .normal)
Upvotes: 0
Reputation: 1089
I did it Like This :
Somewhere, in my Case ViewDidLoad . . .
if(![[NSUserDefaults standardUserDefaults]boolForKey:@"volume_control"]){
[btnVolume setImage:[UIImage imageWithIcon:@"fa-volume-up" backgroundColor:[UIColor clearColor] iconColor:clrPureBlueTint andSize:CGSizeMake(32, 32)] forState:UIControlStateNormal];
}else{
[btnVolume setImage:[UIImage imageWithIcon:@"fa-volume-off" backgroundColor:[UIColor clearColor] iconColor:[UIColor whiteColor] andSize:CGSizeMake(32, 32)] forState:UIControlStateNormal];
}
Then on Click Event
- (void)btnVolumeClicked : (UIButton *)sender{
if ([[NSUserDefaults standardUserDefaults]boolForKey:@"volume_control"]) {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"volume_control"];
[[NSUserDefaults standardUserDefaults] synchronize];
}else{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"volume_control"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[self selectedState];}
In SelectedState Method :
-(void) selectedState{
if(![[NSUserDefaults standardUserDefaults]boolForKey:@"volume_control"]){
[btnVolume setImage:[UIImage imageWithIcon:@"fa-volume-up" backgroundColor:[UIColor clearColor] iconColor:clrPureBlueTint andSize:CGSizeMake(32, 32)] forState:UIControlStateNormal];
}else{
[btnVolume setImage:[UIImage imageWithIcon:@"fa-volume-off" backgroundColor:[UIColor clearColor] iconColor:[UIColor whiteColor] andSize:CGSizeMake(32, 32)] forState:UIControlStateNormal];
}}
i'm Using Font Awesome , But you can simply set button image Like This
[btnVolume setImage:[UIImage imageNamed:@"NO1.png"]
forState:UIControlStateSelected];
Upvotes: 0
Reputation:
First Create objects for the Buttons with "btYesAction" and "btNoAction" in the storyboard.
Now go to viewcontroller.m and paste the below code
<pre>
(IBAction)btYesAction:(id)sender {
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
[button setImage:[UIImage imageNamed:@"NO1.png"]
forState:UIControlStateSelected];
Upvotes: 0
Reputation: 4373
First create a button as a member variable of your view controller. Let's call it UIButton* myButton
.
Then initialize the button like so, and set its image to the image you want it to display originally:
myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[myButton setImage:[UIImage imageNamed:@"OriginalImage"] forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(onClickMyButton) forControlEvents:UIControlEventTouchUpInside];
The third line there will make it so that when the button is clicked, the function "onClickMyButton" is called. So then you write this function, which will change the image to 'NewImage':
-(void) onClickMyButton
{
[myButton setImage:[UIImage imageNamed:@"NewImage"] forState:UIControlStateNormal];
}
And voila! Problem solved :)
Upvotes: 4
Reputation: 3345
Below two methods you can use – setBackgroundImage:forState: – setImage:forState: check the UIButton
Upvotes: 1
Reputation: 771
assuming this is a standard UIButton, you can do the following when initializing the button:
[btnObj setImage:[UIImage imageNamed:@"imgName.png"] forState:UIControlStateHighlighted];
or for the Background image:
[btnObj setBackgroundImage:[UIImage imageNamed:@"imgName.png"] forState:UIControlStateHighlighted];
Upvotes: 11