The status of the highlighted UIButton is not displayed when clicking on the selected UIButton

I want my UIButton to display the highlighted state when I click on a button that is already selected.

Basically, in the selected state, I use the * .png image as my UIButton backgroundImage to give the pressed effect.

But if the button is already in the selected state. When I click on it again, I just don’t see the highlighted state, but it goes directly to the normal state!

Watch the problem β†’ Video problems!

help me please

//0 init UIButton UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, aSide, aSide)]; //1 Give it a backgroundColor [button setBackgroundColor:aColor]; [..] //2 Set titleLabel and its style [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected]; [button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted]; UIImage *shadowImage = [UIImage imageNamed:kBtnShadow]; shadowImage = [shadowImage stretchableImageWithLeftCapWidth:floorf(shadowImage.size.width/2) topCapHeight:floorf(shadowImage.size.height/2)]; [button setBackgroundImage:shadowImage forState: UIControlStateHighlighted]; [button setTitle:aLabel forState: UIControlStateNormal]; //3 Assign tag and Action [button setTag:tag]; [button addTarget:target action:a forControlEvents:UIControlEventTouchUpInside]; 
+8
selected ios uibutton uicontrolevents
source share
3 answers

The various states: UIControlStateNormal , UIControlStateSelected and (UIControlStateSelected | UIControlStateHighlighted) all really different. If you want your shadowImage applied both in the (only) selected state and in the selected + selected state, you should also set:

 [button setBackgroundImage:shadowImage forState:(UIControlStateHighlighted | UIControlStateSelected)] 
+18
source share

In swift it will be:

 button.setBackgroundImage(shadowImage, forState: UIControlState.Selected.union(UIControlState.Highlighted)) 
+5
source share

In Swift v3 (November 2016):

 button.setBackgroundImage(shadowImage, for: UIControlState.selected.union(UIControlState.highlighted)) 
+1
source share

All Articles