UIButton subview in UICollectionViewCell does not change control state

I have a UICollectionView where each UICollectionViewCell has a UIButton as a sub. UIButtons do not respond to malfunctions (their goals start), but the button itself does not change to the selected state (without changing the appearance of the button). I suspect this because the UICollectionViewCell incorrectly redirects its touch events to the button, but I'm not sure. Even if this is the case, how can I set things up so that the state of the button changes correctly in this scenario?

+6
source share
2 answers

UIScrollview (and therefore UICollectionView too) has a property called delaysContentTouches , by default it is set to YES, change it to NO, and your button should be highlighted as intended.

+12
source

If I can offer an alternative, UICollectionView has a cool delegation method called

 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { } 

which can handle clicking on this cell ... if you are looking for specific events that will happen, for example, if the image changes to the state of the button, you can hard-code the code that ... when they press the button, one thing when they release the button do another thing ....

according to the documentation also

UIControlStateSelected The selected state of the control. For many controls, this state does not affect behavior or appearance. But other subclasses (for example, the UISegmentedControl class) may have a different appearance depending on the selected state. You can get and set this value through the selected property.

in a slovenly situation .. for UIButton the "Selected State" does nothing ...

if the button is supposed to fade when it is pressed and it doesn't, then you might have to do it programmatically if, but I'm not quite sure what you are trying to do ...

dimming function is in highlighted state

UIControlStateHighlighted Highlighting the state of the control. The control enters this state when a touch enters and exits during tracking and when a touch event occurs. You can get and set this value through the selected property.

in simple conditions, you touch the highlighted button

to make sure the button changes state correctly, you can do something like this

 [button addTarget:self action:@selector(functionToCall:) forControlEvents:UIControlEventAllTouchEvents]; NSLog(@"Selected: %i", button.selected); NSLog(@"Highlighted: %i", button.highlighted); NSLog(@"Normal State or not: %i", button.state); 

"functionToCall is called when any type of touch even happens with a button, and with this function you can have these 3 NSLogs that will print different UIControlState values ​​on your console, this will show that the button is working correctly and show that it could be an error UIViewCollection, if it is a UICollectionView ... then you have to programmatically smooth the button: 3

hope this helps!

0
source

Source: https://habr.com/ru/post/944243/


All Articles