I have an Text Field
in my layout, which gets filled out, and afterwards when clicking on the UIButton
in the layout, I want to hide the virtual keyboard.
I assume this can be done easily?
I have an Text Field
in my layout, which gets filled out, and afterwards when clicking on the UIButton
in the layout, I want to hide the virtual keyboard.
I assume this can be done easily?
You can put below line in button click action.
self.view.endEditing(true)
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer =
UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
}
Use this Extension.
You can hide the Keyboard by tapping anywhere on the screen or just call dissmissKeyboard() to hide it.
text box means UITextView? if it is UITextView then resignFirstResponder() is not allowed
you have to hide keyboards by clicking on background view