How to Dismiss the Keyboard in iOS

First, drag the ‘Tap Gesture Recognizer’ from Object Library onto Background. Then control-drag from ‘Tap Gesture Recognizer’ to ViewController.swift to add the dismissKeyboard() @IBAction. And last, call the resignFirstResponder() method from within the dismissKeyboard function to then dismiss the keyboard.

@IBAction func dismissKeyboard(_ sender: UITapGestureRecognizer) {