0

hi Im new to programming I was try to recognize the user touch on the screen and close all textfield but when I add a scrollview I won't be able to do that I read a lot in stack overflow like : tap recognizer but I could not do that anymore after adding the textfield I got confused

tell me how can I do it please?

thanks

Mahdi
  • 94
  • 10

3 Answers3

0

for dismiss the keyboard is the subview of UIView, then use

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)

}

for dismiss the keyboard is the subview of UIScrollview, then use in here scroll view observes the userInteraction so "By setting userInteractionEnabled to NO for your scroll view".

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.yourSCrollviewName.endEditing(true)

}

for resign the keyboard in various types, the some other type has already answered in SO

update

for hide the keyboard the create the TapGesture for your scrollview

    self.ScrollView.isUserInteractionEnabled = true
   // ScrollView.keyboardDismissMode = .onDrag
    let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
    tap.numberOfTapsRequired = 1
    self.ScrollView.addGestureRecognizer(tap)

and call the action as

  func doubleTapped() {
    // do something cool here
     self.ScrollView.endEditing(true)
}
Community
  • 1
  • 1
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0
  • You have to add tap gesture on ScrollView
  • Add below code in action of gesture:

    self.yourSCrollviewName.endEditing(true)

Jogendar Choudhary
  • 3,476
  • 1
  • 12
  • 26
  • terminating with uncaught exception of type NSException and go to delegate file – Mahdi Apr 26 '17 at 13:07
  • let tapGesture = UITapGestureRecognizer(target: self, action: #selector(hideKeyboardOnTapped)) tapGesture.numberOfTapsRequired = 1 self.ScrollView.addGestureRecognizer(tapGesture) – Jogendar Choudhary Apr 26 '17 at 13:59
0
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(keyBoardHideOnTap))
tapGesture.numberOfTapsRequired = 1
self.ScrollView.addGestureRecognizer(tapGesture)


func keyBoardHideOnTap() {
    // do something cool here
     self.ScrollView.endEditing(true)
}
Jogendar Choudhary
  • 3,476
  • 1
  • 12
  • 26