All of my programmatically created buttons work except for one

I have 10 programmatically buttons create, they all work except for the first.
Code Block
for image in 1...10 {
      let imageName = String("image\(String(image)).png")
      let imageToDisplay = UIImage(named: imageName)
      let catagoryButton = UIButton(type: UIButton.ButtonType.custom) as UIButton
      catagoryButton.setBackgroundImage(imageToDisplay, for: .normal)
       
      let xCoor = 20 + 60 * CGFloat(image - 1)
      contentWidth += 60
       
      catagoryButton.frame = CGRect(x: xCoor, y: 10, width: 40, height: 40)
      catagoryButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
      catagoryButton.tag = image
      catagoryScrollView.addSubview(catagoryButton)
      print("This Buttons tag is \(catagoryButton.tag)")
    }
    catagoryScrollView.contentSize = CGSize(width: contentWidth + 20, height: 60)


The button action works fine for all of the other buttons, but just incase it just checks for the sender.tag and does a different action depending on the sender.

Code Block
@objc func buttonAction(sender: UIButton!) {
    print("button number \(sender.tag)")
     
    var showThisArray = [CreditCard]()
    var showThisArrayOfValues = [Double]()
     
    if sender.tag == 1 {
      showThisArray = restaurantCards
      showThisArrayOfValues = restaurantCardsValues
      cardReward = "Restaurants"
    }
     
    else if sender.tag == 2 {
      showThisArray = gasCards
      showThisArrayOfValues = gasCardsValues
      cardReward = "Gas Stations"
    }
     
    else if sender.tag == 3 {
      showThisArray = hotelCards
      showThisArrayOfValues = hotelCardsValues
      cardReward = "Hotels"
    }
     
    else if sender.tag == 4 {
      showThisArray = groceryCards
      showThisArrayOfValues = groceryCardsValues
      cardReward = "Grocery Stores"
    }
     
    else if sender.tag == 5 {
      showThisArray = transitCards
      showThisArrayOfValues = transitCardsValues
      cardReward = "Transit Station"
    }
     
    else if sender.tag == 6 {
      showThisArray = wholesaleCards
      showThisArrayOfValues = wholesaleCardsValues
      cardReward = "Wholesalers"
    }
     
    else if sender.tag == 7 {
      showThisArray = drugStoreCards
      showThisArrayOfValues = drugStoreCardsValues
      cardReward = "Drug Stores"
    }
     
    else if sender.tag == 8 {
      showThisArray = entertainmentCards
      showThisArrayOfValues = entertainmentCardsValues
      cardReward = "Entertainment Locations"
    }
       
    else if sender.tag == 9 {
      showThisArray = supplyCards
      showThisArrayOfValues = supplyCardsValues
      cardReward = "Supply Stores"
    }
     
    else if sender.tag == 10 {
      showThisArray = departmentCards
      showThisArrayOfValues = departmentCardsValues
      cardReward = "Department Stores"
    }
    else if sender.tag == 11 {
      showThisArray = otherCards
      showThisArrayOfValues = otherCardValues
      cardReward = "Other Categories"
    }
// More code that is not important
}

I cannot figure out the root of the problem and I need all of the buttons to work

Answered by junkpile in 624578022
The code as shown cannot possibly be triggering the action for button #11. You must be running some different code.

Anyway, I would check the view debugger to see if there’s some other view getting the touches in front of the button, and make sure your button’s frame is what you expect. The default for clipsToBounds is false, so if the frame is messed up a view can have all of its contents visible outside its bounds but it will not receive touches.
Try looking in the debugger and check if the action method is called for the first button and if so what its tag is.

Also note that you are checking tags up to 11 but you only create buttons from 1 to 10 so it seem like the last one wouldn't be triggered.
The button with tag 1 doesn't work but the one with tag 11 actually does work. When I click on the first button it doesn't print the tag, but on the other buttons it does. So the func doesn't work for the first button.
Accepted Answer
The code as shown cannot possibly be triggering the action for button #11. You must be running some different code.

Anyway, I would check the view debugger to see if there’s some other view getting the touches in front of the button, and make sure your button’s frame is what you expect. The default for clipsToBounds is false, so if the frame is messed up a view can have all of its contents visible outside its bounds but it will not receive touches.
I had a stack view covering the button. The stack view was taking the taps
All of my programmatically created buttons work except for one
 
 
Q