How to Disable/Enable the Return Key in a Uitextfield

Enable Return Key in empty UITextView

I need to enable Return Key on UITextView even if UITextView is empty.

Actually, the return key is enabled by default no matter the text view is empty or not. However, if you want to set the return key to disabled by default, you need to set the enablesReturnKeyAutomatically to YES. As the Apple Doc said:

The default value for this property is NO. If you set it to YES, the keyboard disables the return key when the text entry area contains no text. As soon as the user enters any text, the return key is automatically enabled.

Disable Return key in keyboard for TextView

You can try this:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
return text != "\n"
}

Hope it helps!!



Related Topics



Leave a reply



Submit