I have several UIButtons within UICollectionViewCells and I am unable to make them react to show a highlighted image as they do outside a UICollectionView.
There seems to be a delay and the highlighted image is only shown if you long press down on the button. This is a problem I have experienced before and would like to get to the bottom of it.
This is the code for setting a standard button image for normal and highlighted states.
// inside GeneralCollectionViewCell
let generalButton: UIButton = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.setImage(UIImage(named: "general_button")?.withRenderingMode(.alwaysOriginal), for: .normal)
button.setImage(UIImage(named: "general_button_highlighted")?.withRenderingMode(.alwaysOriginal), for: .highlighted)
return button
}()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellIdentifer", for: indexPath) as! GeneralCollectionViewCell
cell.addTarget(self, action: #selector(generalButtonHandler(_:)), for: .touchUpInside)
return cell
}
Any help on how to fix this annoying problem would be great.