iosobjective-ciphoneuibuttonuicontrolstate

UIButton - On touch change image


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];
            }

Solution

  • 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.