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)
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With