In the past, to dismiss the keyboard after a user hits return I would simply override the TextViewShouldReturn function. However, upon re-downloading Xcode (version 9.3.1) it seems it is no longer a function within UITextViewDelegate and most all previous questions I've found on the subject advise overriding TextViewShouldReturn in some way as well. Is there something I've forgotten or a more efficient way possibly?
Asked
Active
Viewed 1,112 times
4 Answers
3
There is no method like textViewShouldReturn() for textView like textField, rather you can use the following code to return your keyboard-
extension ViewController: UITextViewDelegate {
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if(text == "\n") {
textView.resignFirstResponder()
return true
}
return true
}
// Make sure you have self the textView delegate in viewDidLoad method

Vikash Kumar
- 642
- 1
- 11
- 25

DEEPAK KUMAR
- 341
- 4
- 8
2
Maybe you're confusing UITextViewDelegate
with UITextFieldDelegate
?

Eimantas
- 48,927
- 17
- 132
- 168
-
You're totally right. Always the silly mistakes. Thanks! – Studwell May 22 '18 at 07:52
1
There is no function textViewShouldReturn
in UITextViewDelegates this function is of UITextField delegate so you have to create your own code for hide keyboard when click on return. By default in UITextView return button is used for next line.

Gurinder Batth
- 655
- 9
- 18
-1
if you want to hide keyboard .try this
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
if ( [ text isEqualToString: @"\n" ] ) {
[ textView resignFirstResponder ];
return NO;
}
return YES;
}

Jigar
- 1,801
- 1
- 14
- 29