Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gesture recogniser not working when added button view to the Label view as subview

Tags:

ios

swift

I Used a stack view to render labels in it and then added the buttons to the respective labels, used a target to handle the event when button is tapped. Here the handler is not called when added as subview to the label

Stackview

label1 
      button1

label2 
      button2

label3
      button3

Function to add button to the labelview

 func setButton(count:Int){

    for i in 0..<count{

        buttons[i].tag = i

        buttons[i].addTarget(self, action: #selector(handleDelete(_:)), for: .touchUpInside)

        label[i].addSubview(buttons[i])

    }

Handler

@objc func handleDelete(_ sender:UIButton){

    print(sender.tag)
}
like image 774
Swamy Avatar asked Dec 06 '25 20:12

Swamy


1 Answers

Set the labels userInteraction to true.

Another better solution is to add the button outside the label, so both the label and button are in a container view.

like image 115
User123335511231 Avatar answered Dec 09 '25 08:12

User123335511231