UIButton displays the selected state when pressed

How to show selected state for UIButton when clicked?

I want that when people click on uibutton, it shows the selected hover state that I have?

At the moment, they must hold the button to see the hang.

[t1 setBackgroundImage: [UIImage imageNamed: [NSString stringWithFormat: @"hover.png"]] forState:UIControlStateSelected];

I also want to show a hover image.

+5
source share
2 answers

You need to set the selected state for the button

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setImage:[UIImage imageNamed:@"hover.png"] forState:UIControlStateSelected];

To display the image in hoover mode when you click the button, you need to set the state property UIControlStateHighlighted

0
source

I had the same problem - my button was highlighted when pressed (tap + hold), and was not - just by tapping. Here is my code:

btn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *image =[UIImage imageNamed:@"btn"];
        UIImage *imageSelected =[UIImage imageNamed:@"btn_tap"];
        [btn setBackgroundImage:transImage forState:UIControlStateNormal];
        [btn setBackgroundImage:imageSelected forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];

-(void)btnPressedWithDelay{
    //your code for btn action
}

- (void) action:(UIButton *)sender{
    [self performSelector:@selector(btnPressedWithDelay) withObject:nil afterDelay:0.1];
}

?

+1

All Articles