In viewDidLoad:
//Dissmiss Keyboard
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
view.addGestureRecognizer(tap)
Function:
//Dismiss Keyboard
@objc func dismissKeyboard() {
view.endEditing(true)
}