title: How to dismiss keyboard when touching anywhere outside UITextField (in swift)?
body: <p>I'm working on a project that have a UIViewController, on the view controller there is a UIScrollView and a UITextField on the scrollview.
  like this:
  <a href="https://i.stack.imgur.com/E1RPH.png"><img src="https://i.stack.imgur.com/E1RPH.png" alt=""></a>
  I'm trying to dismiss the keyboard and hide it after typing some text in the textfield and tap anywhere outside the textfield.
  I've tried the following code:</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
      self.textField.delegate = self;
  }
  
  override func touchesBegan(touches: Set&lt;UITouch&gt;, withEvent event: UIEvent?) {
      self.view.endEditing(true)
  }
  </code></pre>
  
  <p>It works for me when I tap outside the scrollview, but when I tap on the scrollview nothing happens and the keyboard doesn't hide.</p>
  
  <p>Is there any way to dismiss the keyboard when tapping <strong>anywhere</strong> outside the textfield?
  thanks</p>