0

I have tab in an iOS app that shows a table view, but also a keyboard to let you add more content. I'd like a way to setup a gesture recognizer such that if you click anywhere on white space, it'll dismiss the keyboard, however, if you click on an image or anything that has content, it'll still allow those actions.

When I setup the gesture tap recognizer, it seems to take over all the events and although it dismisses the keyboard, when I click on an image none of those actions resolve. Any ideas? I'm using Swift.

demig0d
  • 1,131
  • 1
  • 12
  • 24
  • can you show us some code also in the tapgesturerecognizer you have why not set it to self.view so then if an image is pressed it's not the view itself – Konsy Jul 19 '16 at 19:25
  • implement `touchesBegan` on the controller's view and call `endEditing(true)` in it. Or use a tap recognizer and in its delegate you can decide when it should receive touch. – Sulthan Jul 19 '16 at 19:41
  • Please refer this url :: [dismiss-keyboard-on-touch-anywhere-outside-uitextfield](http://stackoverflow.com/questions/11274119/dismiss-keyboard-on-touch-anywhere-outside-uitextfield) – Chandan Jul 19 '16 at 19:41

1 Answers1

1

Adding this inside viewDidLoad() worked for me (Swift 3):

let tap = UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:)))
tap.cancelsTouchesInView = false
self.view.addGestureRecognizer(tap)