title: Make keyboard disappear when clicking outside of Search Bar - SWIFT
body: <p>I have a UIViewController and I have embedded a Search Bar and a Collection View.  When I press on the searchbar, the keyboard appears.  I would like to hide this keyboard if the user decides to change his mind by tapping any where on the screen but the search bar.  I have tried the following without success:</p>
  
  <ol>
  <li>Adding a Tap Gesture Recognizer</li>
  <li><p>using 'self.mySearchBar.endEditing(true)'</p>
  
  <pre><code>class CollectionViewFolder: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate ,UICollectionViewDelegateFlowLayout, UISearchBarDelegate{
  
  /*** OUTLETS ***/
  @IBOutlet weak var mySearchBar: UISearchBar!
  
  
  // 1. I have tried adding a Tap Gesture Recognizer
  // TAP ON SCREEN LISTENER
  @IBAction func tapOnScreen(_ sender: UITapGestureRecognizer) {
     print("tap tap ...")
     self.mySearchBar.resignFirstResponder()
  }
  
  
  
  // 2.  Added the following to the viewDidLoad:
  override func viewDidLoad() {
     super.viewDidLoad()
  
     self.mySearchBar.endEditing(true)
   }
  
  }
  </code></pre></li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/MQjaV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MQjaV.png" alt="enter image description here"></a></p>