So this is a tvOS project in Swift. I have a custom UICollectionViewCell with a button as one of its subviews. I add a target to the button to allow it to interpret clicks. Here's a simplified version of the relevant code
class CustomCell: UICollectionViewCell {
var button:UIButton!
override init(frame: CGRect) {
super.init(frame: frame)
button = UIButton(...) // Button is initialized with a frame
button.userInteractionEnabled = true
button.enabled = true
button.addTarget(self, action: "pressed:", forControlEvents: .PrimaryActionTriggered)
contentView.addSubview(button)
}
func pressed(sender: UIButton!) {
print("button pressed!")
}
}
For some reason it's not ever printing out my message. I tried adding 'pressedEnded' to the cell class to see if it's getting any and it gets called
func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
// If I put a breakpoint here it's reached
}
Any suggestions? When I run it in the simulator the button can get focus, so I don't know why it can't get any events
It seems the UICollectionViewCell is overlayed automatically by a clear view, which catches taps.
For us just calling bringSubview(toFront: checkButton) worked. Now the checkButton touchUpInside action is called as it should.
Make sure the User Interaction Enabled checkbox is checked for the UICollectionViewCell.
So, I figured out how to solve this, although it's somewhat of a workaround. Basically for the
UICollectionView
I need to ensure the cell's can't get focus.Next I had
didUpdateFocusInContext
inCustomCell
previously. This was what was actually animating the button when the cell, but when I checked the button never got focus. I'm guessing this was intercepting it. So I removed that function fromCustomCell
and instead, at the bottom of my file I added that function as an extension of UIButton.This also could've been done by creating a subclass of
UIButton
and using that instead, but this was less code (that is probably the ideal way). So the full code looks like:Above answer is correct, and if you are using a custom
UICollectionViewCell
you could also do this in its subclass for specific cells: