How to disable/enable the return key in a UITextField?
Is there a way to programmatically enable or disable the Return Key on the UIKeyboard? The closest I could find is enablesReturnKeyAutomatically, but that only will tell whether to disable it at all.
This is publicly available in iPhone SDK in UITextInputTraits. Using this, return key will be disabled when no input text is available within text field.
UITextField's enablesReturnKeyAutomatically property can be set right in Interface Builder, just select the textfield and open the Attributes inspector. As Tharindu stated, this will automatically enable and disable the return key depending on whether any text has been entered.
Of course, if you need to change this in code you can still set it programmatically using nameTextField.enablesReturnKeyAutomatically = true.
EDIT to address the downvotes:
Otherwise, there is no official way to enable and disable the return key on command. I would recommend against trying to use private APIs to accomplish this. Alternatively, you can use the textFieldShouldReturn: delegate method and put your conditional/validation there and respond accordingly.
You can override UITextField's hasText attribute to achieve this:
class CustomTextField : UITextField {
override public var hasText: Bool {
get {
return evaluateString(text)
}
}
}
Where evaluateString(_ text: String?) -> Bool checks against your needed input criteria, for example character count.
Of course this does only work in combination with enablesReturnKeyAutomatically = true set on the UITextField.
I am aware that my answer is neither timely nor written in Objective-C, but given that I have not been able to find an answer anywhere else and this question being routinely referred to in other threads, I think that here is the best place to post it.