Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • 7
    Great piece of code, thanks! touchesBegan was exactly what I was looking for :) Commented Jul 1, 2015 at 22:42
  • 4
    This didn't quite work for me. If I tap on the view controller's view it works. If I tap on a control item in the view, the keyboard doesn't dismiss. Commented Apr 9, 2016 at 18:58
  • This is the best solution I have seen so far. Small note: in newer Swift the override signature changed a bit. You need to add _ before touches: override func touchesBegan(_ touches: Set<UITouch>, withEvent event: UIEvent?) Commented Dec 12, 2018 at 19:57
  • 1
    This one should probably be marked as correct one. The current accepted answer makes every button in the view not responsive when keyboard is showing. Commented Mar 7, 2019 at 13:37
  • This doesn't work when tapping inside a tableview or any scrollable. Commented Jun 10, 2019 at 15:40