Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UIButton image when button pressed

I'm trying to change the image button just in the moment that the button is pressed. When released, i want go back to the original.

I read some posts here, but it's not working.

Is it possible do it in the GUI without programming?

I tried this:

-(IBAction) toggleUIButtonImage:(id)sender{
     if ([sender isSelected]) {
         [sender setImage:unselectedImage forState:UIControlStateNormal];
         [sender setSelected:NO];
     }else {
         [sender setImage:selectedImage forState:UIControlStateSelected];
         [sender setSelected:YES];
      }
 } 

But what kind of sender events should i associated the function toggleUIButtonImage ?

Thanks

like image 765
John Avatar asked Nov 30 '22 22:11

John


1 Answers

You can do it in interface builder by giving different settings to each button state config:

The available states are: default, selected and disabled

Or you can also change the 'settings' of the button in code by using 'UIButton' setImage:forState method.

    [myButton setImage:highlightedImage forState:UIControlStateHighlighted];

Usually, you should do it by 'configuring' the button as described above and not by changing the sub views of the button manually as a response to user interaction. The reason it doesn't work for you is the inherent hidden behaviour of UIButton. You manually make the change of a subview of the button as a response to user interaction, but your change is changed back to what was defined in the settings of your button (according to the button's state). So it seems like your code doesn't do anything at all.

like image 169
PostPCDev Avatar answered Dec 03 '22 12:12

PostPCDev