When I touch the button at that time I want to change image & when i release the touch button image is as it is.
I want to apply below code but it's not with my expectation.
please give me any suggestion.....
-(IBAction)actionEnter:(id)sender{
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:@"enter-hover.png"]
forState:UIControlStateNormal];
[sender setSelected:NO];
} else {
[sender setImage:[UIImage imageNamed:@"enter.png"]
forState:UIControlStateSelected];
[sender setSelected:YES];
}
to change the image immediately use the backgroundImage property.
In Swift:
button.setImage(UIImage(named: "enter.png"), forState: [.Selected, .Highlighted])
You can use UIControlStateHighlighted for this.
You can also set this from interface builder by setting the image for highlighted state.
I think, you could set the image in the beginning for normal and selected state ..
Try with below when you create the
UIButton
object. [Use the images as per your requirement]I think this should do it. Set the images after creating the button
and do this
@7KV7 got me thinking. I have favorite and ignore buttons that I want to use to mark favorite pictures and pictures that I never want to see again. I used his method to initialize the buttons and then slightly modified his method to toggle the buttons on and off.
In this example, if you mark a picture as a favorite, you want to turn off the ignore button and vice versa. The delegate handles the database stuff.
If you are just toggling a button on or off, you won’t need to make it a property, since the buttonPressed sender knows which button has been pressed. I need to have them be property since I need to tell the opposite button to turn its highlight off.