There is lots of answers for how to make the keyboard go away when you press the return key, but I want the functionality of the return key still so users can make new lines but I also need a way to still close the keyboard.
Some things I have considered are Gesture Recognizers to close the keyboard, but that might not be intuitive. Thoughts and best practices here are appreciated.
Please note before answering I already have a Gesture Recognizer if the user clicks outside the UITextView to close any keyboards but this particular UITextView takes up the entire screen and as such tapping in it doesn't work.
My current code I'm using to do that was taken from another post and looks like below.
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
view.endEditing(true)
}
}
All I have to do is call this function in my viewcontroller where I want the tap to close the keyboards for me anywhere from any text controls. It's been working great so far, but clicking on the controls themselves still creates an issue in which the keyboard does not go away.
What is the best way to close the keyboard for a UITextView that takes up the full screen?
I propose you to add toolbar with "Done" button above keyboard. There are a lot of tutorials how to do this, no reason to copy SO answers, just check: How to add a 'Done' button to numpad keyboard in iOS
Possible Solution Here
To use it simply call addDoneButton on any UITextView from now on.