// Swift 4
extension UIViewController {
func automaticallyDismissKeyboard() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
view.addGestureRecognizer(tapGesture)
}
@objc private func dismissKeyboard() {
view.endEditing(true)
}
}