2019-06-19
How do i dismiss the keyboard on return key from a UITextView
stackoverflow
Question

How do I dismiss the keyboard by pressing the return key on device? I know how to dismiss keyboard, but not when using the UITextView:

resignFirstResponder

I have tried this, but it does not work:

self.messageTextView.delegate = self

And with this function:

func messageTextViewShouldReturn(textView: UITextView) -> Bool
    {
        self.messageTextView.resignFirstResponder()
        return true
    }
Answer
1

Remember to add the UITextDelegate into ViewDidLoad()

 func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    if text == "\n"
    {
        textView.resignFirstResponder()
        return false
    }
    return true
}
How do i dismiss the keyboard on return key from a UITextView
See more ...