Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton changes states when being pressed

I have an UIButton configured with two states, default and selected, for different text and background, the set up is done on Interface Builder. The weird thing is when the button is in selected state, and the button is pressed, it automatically changes to default state. After the button is released, it changes back to selected state. Is there way to disable roll-back-to-default behavior?

Forgot to mention that, the type is custom.

Update

tried the following code in viewDidLoad

[self.button setTitle:@"default" forState:UIControlStateNormal];
[self.button setTitle:@"selected" forState:UIControlStateSelected];
[self.button setTitle:@"highlighted" forState:UIControlStateHighlighted];
[self.button setSelected:YES];

The button shows default when pressed.

like image 459
Dino Tw Avatar asked Apr 21 '14 22:04

Dino Tw


1 Answers

Selected state and Highlighted state are not mutually exclusive. When you press a selected UIButton, it gets into a selected AND highlighted state.

I don't think there's a way to set that in the IB, but you can do this in code:

[button setImage:[UIImage imageNamed:@"some_image"] forState:UIControlStateSelected | UIControlStateHighlighted];
like image 176
Joseph Lin Avatar answered Sep 28 '22 14:09

Joseph Lin