add tap gesture to a UILabel Programmatically
Asked Answered
J

4

5

I am trying to add tap gesture for a dynamically created UILabel in a function in swift 4, but it is not firing UITapGestureRecognizer function. it is working when i add tap gesture from viewDidLoad function, but i have to add tap gesture from other function.

here is the code

    override func viewDidLoad() {

    super.viewDidLoad()

    createLabel()
    }

   func createLabel() {

   let label = UILabel()
   label.text = "abc"
   label.numberOfLines = 0 
   label.frame.size.width = self.otherlinksStack.bounds.width
   label.font = label.font.withSize(17) // my UIFont extension

   label.sizeToFit()
   label.tag = 1
   self.otherlinksStack.addSubview(label)
   let labelTapGesture = UITapGestureRecognizer(target:self,action:#selector(self.doSomethingOnTap))

 label.isUserInteractionEnabled = true

 label.addGestureRecognizer(labelTapGesture)

 }

 @objc func doSomethingOnTap() {

    print("tapped")
}
Judy answered 14/2, 2019 at 12:17 Comment(13)
Add code in to questionParfleche
Well, did you set userInteractionEnabled to true?Preposition
yes, i set userInteractionEnabled to true. but it not workingJudy
You need to provide much more information - see How to Ask and minimal reproducible exampleBibliopegy
please see the code in the question. I edited it.Judy
Is user interaction on your otherlinkstack enabled?Nenitanenney
otherlinkstack is a UIStackView and its user interaction is enable.Judy
@abdul-qayyum: If you have your UIStackView user interaction enabeled your label should take tapNenitanenney
@SandeepBhandari yes, but it is not working for me.Judy
@AbdulQayyum - is otherlinksStack a UIStackView?Bibliopegy
@Bibliopegy Yes, it is a UIStackViewJudy
@AbdulQayyum - is your stack view's Alignment set to Fill?Bibliopegy
@AbdulQayyum - actually, Alignment doesn't matter. See my answer.Bibliopegy
B
5

You're doing a couple things wrong...

// your code
label.frame.size.width = self.otherlinksStack.bounds.width
label.sizeToFit()

If you're adding the label to a stackView, there is no need to set its frame -- let the stack view handle that. If your stackView's alignment is set to .fill it will stretch the label to its width anyway. If it's not set to fill, the label will expand horizontally as needed, based on its text. So, also, no need to call .sizeToFit().

// your code
self.otherlinksStack.addSubview(label)

When add a view to a stack view, use .addArrangedSubview, otherwise it will be added overlaid on top of another view in the stack view.

This should work fine (it does in my quick test):

func createLabel() {

    let label = UILabel()

    label.text = "abc"
    label.numberOfLines = 0
    label.font = label.font.withSize(17) // my UIFont extension
    label.tag = 1

    // give the label a background color so we can see it
    label.backgroundColor = .cyan

    // enable user interaction on the label
    label.isUserInteractionEnabled = true

    // add the label as an Arranged Subview to the stack view
    self.otherlinksStack.addArrangedSubview(label)

    // create the gesture recognizer
    let labelTapGesture = UITapGestureRecognizer(target:self,action:#selector(self.doSomethingOnTap))

    // add it to the label
    label.addGestureRecognizer(labelTapGesture)

}

@objc func doSomethingOnTap() {
    print("tapped")
}
Bibliopegy answered 14/2, 2019 at 17:30 Comment(0)
P
2
func addTapGesture() {
  let labelTapGesture = UITapGestureRecognizer(target: self, action: #selector(doSomethingOnTap))
  //Add this line to enable user interaction on your label 
  myLabel.isUserInteractionEnabled = true
  myLabel.addGestureRecognizer(labelTapGesture)
}
    
@objc func doSomethingOnTap() {
  print("tapped")
}

Then call addTapGesture() from viewDidLoad or from whichever function you wanna add tap from.

Phytology answered 14/2, 2019 at 12:53 Comment(1)
no, its not working. i am creating UILabel programmatically and add tap gesture with in a function that is called from viewDidLoad.Judy
H
1

Use this code for adding the Tap Gesture:

let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapGestureMethod(_:)))
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1
yourLabel.isUserInteractionEnabled = true
yourLabel.addGestureRecognizer(tapGesture)

This is the tap gesture method:

@objc func tapGestureMethod(_ gesture: UITapGestureRecognizer) {
    Do your code here
}
Hypochondriac answered 14/2, 2019 at 13:53 Comment(0)
P
0

If your gesture works from viewDidLoad, then after adding from other function the gesture might override by other control's gestures..

Try adding gesture on control which is seperate from all other controls.

Pneumatics answered 14/2, 2019 at 12:37 Comment(1)
The OP is creating the label -- how much more separate can it be?Ez

© 2022 - 2024 — McMap. All rights reserved.