skip to Main Content

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


  1. Where have you placed this @IBAction?

    1. Inside your UICollectionViewCell subclass
      OR
    2. Inside your UIViewController subclass

    It 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 using isUserInteractionEnabled property.


    Use View Hierarchy Debugger to make sure your button is on top of all other subviews at run time and has non zero
    width / 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 enter image description here

    I can see two plus buttons,

    1. one inside the ProductCollectionViewCell > Content View
    2. other inside the View Controller > View, right between Total Label & Navigation Item.

    You should make sure that you connect the first one to @IBAction inside the ProductCollectionViewCell 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.
    enter image description here

    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 above Constraints and below Stock Cell.

    This will ensure that no other views are overlapping the button.

    As for the UIButtonLabel that you are seeing, that is internal UIKit class that you can’t control and you won’t have issues because of that.


    Login or Signup to reply.
  2. 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.

    • Don’t forget to give it the constraints (Trailing, Leading, Top & bottom)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search