Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITapGestureRecognizer conflict with didSelectRowAtIndexPath

well before i ask this i try the other solutions and it didn't work

this is my code

override func viewDidLoad() {
    super.viewDidLoad()

    let tapGesture = UITapGestureRecognizer(target: self, action: "didTouchBoard:")
    view.addGestureRecognizer(tapGesture)
    tapGesture.cancelsTouchesInView = true
}

when i click anywhere it will hide the keyboard if exist , but if i click on tableviewcell or collectionviewcell , it won't click

i try this tapGesture.cancelsTouchesInView , but not work

like image 711
Ahmed Safadi Avatar asked Feb 07 '23 12:02

Ahmed Safadi


2 Answers

If you want table view to receive touch, then change tapGesture.cancelsTouchesInView

tapGesture.cancelsTouchesInView = false
like image 147
Sasha Kozachuk Avatar answered Mar 24 '23 19:03

Sasha Kozachuk


You have to add the UITextFeildDelegate methods to enable and disable tap gesture in the view

  1. declare tapGesture outside the view did load method

let tapGesture = UITapGestureRecognizer(target: self, action: "didTouchBoard:")

  1. implement the following methods

     func textFieldDidBeginEditing(textField: UITextField) {
    self.view.addGestureRecognizer(self.tapGesture) }
    

    func textFieldDidEndEditing(textField: UITextField) { self.view.removeGestureRecognizer(self.tapGesture) }

i think that might work for you

like image 38
Sucharu Hasija Avatar answered Mar 24 '23 17:03

Sucharu Hasija