title: Swift 3.0 How to dismiss keyboard on a Search Bar when clicking outside of the bar
body: <p>As stated in the title, I am interested in dismissing the keyboard from a search bar when clicking outside of the search bar. Similar questions stated to used self.view.endEditing(true) in touchesBegan, but I have had no luck. I am not sure if I am missing anything else, or if I have set up my searchBar correctly. </p>
  
  <pre><code>import UIKit
  import MapKit
  
  class AddressVC: UIViewController {
  
      @IBOutlet weak var searchBar: UISearchBar!
      var searchCompleter = MKLocalSearchCompleter()
      var searchResults = [MKLocalSearchCompletion]()
  
  
  
  
      @IBOutlet weak var searchResultsTableView: UITableView!
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          searchBar.delegate = self
      // Do any additional setup after loading the view, typically from a nib.
  
          searchResultsTableView.isHidden = true
  
          searchCompleter.delegate = self
      }
  
  
  
  
  extension AddressVC: UISearchBarDelegate {
  
  func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  
      searchCompleter.queryFragment = searchText
  }
  
  func searchBarShouldEndEditing(_ searchBar: UISearchBar) -&gt; Bool {
      self.searchBar.endEditing(true)
      searchBar.resignFirstResponder()
      return true
  }
  
  func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -&gt; Bool {
      searchResultsTableView.isHidden = false
      return true
  
  }
  
  func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
      self.view.endEditing(true)
      self.searchBar.endEditing(true)
      searchBar.resignFirstResponder()
  }
  
  func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
      searchResultsTableView.isHidden = true
      self.searchBar.endEditing(true)
      searchBar.resignFirstResponder()
  
  }
  
  func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
      searchBar.resignFirstResponder()
  }
  
  
  }
  </code></pre>