UIButton image change at the click of a button

I try to change the image button only when the button is pressed. When released, I want to return to the original.

I read a few posts here, but it does not work.

Is it possible to do this in a graphical interface 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 sender events should be associated with the toggleUIButtonImage function?

thanks

+4
source share
4 answers

You can do this in the interface builder by setting different settings for each button state:

The available states are: default, selected and disabled

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

  [myButton setImage:highlightedImage forState:UIControlStateHighlighted]; 

Normally, you should do this by “customizing” the button as described above, and not by manually changing the submenu of the button as a response to user interaction. The reason it doesn't work for you is because of the hidden behavior of UIButton. You manually make changes to the subtitle of the button in response to user interaction, but your change changes to what was defined in the settings of your button (in accordance with the state of the button). So it seems that your code is not doing anything.

+12
source

in the ViewDidLoad method

set property

 [[button setImage:[UIImage ImageNamed:@"unselectedImage.png"] forState: UIControlStateNormal]; [[button setImage:[UIImage ImageNamed:@"selectedImage.png"] forState: UIControlStateHighlighted ]; 
+4
source

This can be done using Interface Builder, without the need for code.

Select your button in IB, go to the right sidebar, change the type to custom, make sure that State Config is the default, and enter the name of your link "regular image" in the text box "Background image".

Then change the state configuration to highlighted and enter the name of the click link in the Background Image text box.

+2
source

In IB, you can set the image for the selected (not selected) button state. That should do it.

Look at the "Button" section of the "Utilities" panel and select "Highlighted" from the "Status Configuration" drop-down list.

+1
source

Source: https://habr.com/ru/post/1439449/


All Articles