nambatee
nambatee

Reputation: 1568

Change UIReturnKeyType on UITextView change

I'm trying to change the return key from default to done when the user enters a string inside my UITextView. I'm attempting to do this inside textViewDidChange(_ textView: UITextView) but the return key changes only after I tap inside another UITextView (with a different return key type) and come back to the original text view. It does not happen immediately, after the string has been entered.

func textViewDidChange(_ textView: UITextView) {
    myTextView.returnKeyType = myTextView.text.isEmpty ? .default : .done
}

What am I doing wrong?

Upvotes: 0

Views: 459

Answers (3)

nambatee
nambatee

Reputation: 1568

I solved it by setting myTextView.enablesReturnKeyAutomatically = true. It grays out the 'Done' button and makes it blue when the user starts typing!

Upvotes: 0

Navneet Gill
Navneet Gill

Reputation: 393

You cant update the keyboard once it is open. You can update it by just closing and opening at the same time. So put this at the bottom

self.view.endEditing(true)
textView.becomeFirstResponder()

Upvotes: 2

Shehata Gamal
Shehata Gamal

Reputation: 100503

Try this

func textViewDidChange(_ textView: UITextView) {

   textView.returnKeyType = ert..text.isEmpty ? .default : .done

   textView.resignFirstResponder()

   textView.becomeFirstResponder()
}

Upvotes: 2

Related Questions