Paul
Paul

Reputation: 789

ios - UIButton highlight setting act weird

My highlighted images show different. Here is my situation.

[button setImage:"normal image" forState:UIControlStateNormal];
[button setImage:"hover image" forState:UIControlStateHighlighted];
[button setImage:"hover image" forState:UIControlStateSelected];

- (IBAction)buttonTouched:(id)sender {
    UIButton *selectButton = sender;


    if (_currentSelectedTabButton.tag == selectButton.tag) {
        //TODO refresh view controller
        return ;
    }

    _currentSelectedTabButton.selected = NO;
    selectButton.selected = YES;

    _currentSelectedTabButton = selectButton;
}
  1. Touch button.
  2. Button shows highlighted image. (correct image)
  3. Button's state is set to "selected"
  4. Touch button.
  5. Button shows different highlighted image.

When a button is in selected state, highlighted images show incorrectly. Any ideas?

Upvotes: 2

Views: 1184

Answers (2)

RockLee
RockLee

Reputation: 11

UIControlStateSelected | UIControlStateHighlighted is a state is independent of UIControlStateHighLighed

Upvotes: 0

Nikita Sharma Sahu
Nikita Sharma Sahu

Reputation: 335

This is because you are setting both the setImage:forControllState:UIControlStatesSelected and setHighlighted image and when you press button for first time its state being unselected the highlighted image is displayed and upon again pressing the button it shows the selected image since now the button is in selected state. (in short highlighted image will work in non-selected state of button only).

Upvotes: 2

Related Questions