I added a button to my collection view, but I cannot press it. I coded it so it would print something to the console as a test, but it still didn’t work. The UIButton is connected to the respective class as well.
This is the code:
@IBAction func plusTapped(_ sender: UIButton) {
print("presses!")
}
2
Answers
Where have you placed this
@IBAction
?UICollectionViewCell
subclassOR
UIViewController
subclassIt should be in #1
Please check if your button allows user interaction.
Check inside storyboard for
User Interaction Enabled
checkbox, it should be checked.Make sure you are not changing this to
false
from code usingisUserInteractionEnabled
property.Use
View Hierarchy Debugger
to make sure your button is on top of all other subviews at run time and has non zerowidth
/height
.Maybe try disconncting this action from storyboard and reconnecting again? Could be a signature mismatch because of copy paste code.
UPDATE
In the attached screenshot
I can see two plus buttons,
ProductCollectionViewCell > Content View
View Controller > View
, right between Total Label & Navigation Item.You should make sure that you connect the first one to
@IBAction
inside theProductCollectionViewCell
and NOT the second one.UPDATE 2 – For the EDIT in question
In the attached screenshot – we can see that the plus button is below other views in the hierarchy inside the
Content View
.You can try moving this to be on top of all other views within the
Content View
by dragging along the red-arrow markup so that it is present aboveConstraints
and belowStock Cell
.This will ensure that no other views are overlapping the button.
As for the
UIButtonLabel
that you are seeing, that is internalUIKit
class that you can’t control and you won’t have issues because of that.I think you need to make it far away a little bit from your
imageView
to make sure that there is no conflict between them, try it and give it a fixed width and height and it will work correctly, so all you need to do is to drag the image to the bottom till being separated from the button.