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];
}
You can use UIControlStateHighlighted for this.
[myButton setImage:[UIImage imageNamed:@"enter-hover.png"]
forState:UIControlStateHighlighted];
You can also set this from interface builder by setting the image for highlighted state.
I think this should do it. Set the images after creating the button
[yourButton setImage:[UIImage imageNamed:@"enter-hover.png"]
forState:UIControlStateSelected];
[yourButton setImage:[UIImage imageNamed:@"enter.png"]
forState:UIControlStateNormal];
and do this
- (IBAction)actionEnter:(id)sender{
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
}
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]
[myButton setImage:[UIImage imageNamed:@"enter.png"]
forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:@"enter-hover.png"]
forState:UIControlStateSelected];
In Swift:
button.setImage(UIImage(named: "enter.png"), forState: [.Selected, .Highlighted])
@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.
self.favoriteButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.ignoreButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.favoriteButton setImage:[UIImage imageNamed:@"Favorite-Selected"]
forState:UIControlStateSelected];
[self.favoriteButton setImage:[UIImage imageNamed:@"Favorite"]
forState:UIControlStateNormal];
[self.ignoreButton setImage:[UIImage imageNamed:@"Ignore-Selected"]
forState:UIControlStateSelected];
[self.ignoreButton setImage:[UIImage imageNamed:@"Ignore"]
forState:UIControlStateNormal];
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.
- (void)favoriteIgnore:(UIButton *)buttonPressed {
// Toggle the tapped button
buttonPressed.selected = ( buttonPressed.selected) ? NO : YES;
id <ScoringToolbarDelegate> TB_delegate = _delegate;
// Turn off the other button and call the delegate
if ([buttonPressed.currentTitle isEqualToString:@"favorite"]) {
self.ignoreButton.selected = NO;
[TB_delegate favoriteButtonPressed];
} else {
self.favoriteButton.selected = NO;
[TB_delegate ignoreButtonPressed];
}
}
to change the image immediately use the backgroundImage property.