3

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")
}
Abdul Qayyum
  • 77
  • 1
  • 6

4 Answers4

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")
}
DonMag
  • 69,424
  • 5
  • 50
  • 86
1
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.

aturan23
  • 4,798
  • 4
  • 28
  • 52
Archita
  • 11
  • 3
  • no, its not working. i am creating UILabel programmatically and add tap gesture with in a function that is called from viewDidLoad. – Abdul Qayyum Feb 14 '19 at 13:35
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
}
Anshul Bhatheja
  • 673
  • 3
  • 21
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.

Manish
  • 608
  • 1
  • 11
  • 23