I need to disable the "Go" button in my UIKeyboard
until a certain condition is satisfied(text in UITextField
must match a string in a saved array). I have tried _theSearch.enablesReturnKeyAutomatically=NO;
inside the method -(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
but that doesn't work. What can be done here?

- 23,586
- 12
- 103
- 136

- 927
- 1
- 6
- 25
-
possible duplicate of [disable/enable return key in UITextField?](http://stackoverflow.com/questions/788323/disable-enable-return-key-in-uitextfield) – Anbu.Karthik May 11 '15 at 06:14
-
Tried that method and it didn't work – Sidharth J Dev May 11 '15 at 06:19
-
show your condition bro – Anbu.Karthik May 11 '15 at 06:27
-
Sorry bro..There is no public API for doing that. – Tapas Pal May 11 '15 at 06:37
3 Answers
There is no other known public API to disable the GO key.
If you want to use conditions then you should implement the delegate
method textFieldShouldReturn
. Return true
when the conditions are met and perform whatever the GO button is supposed to be doing.
Otherwise just return false
and perhaps inform the user of the problem.

- 13,273
- 1
- 38
- 44
-
1This will not work. He want to disable the "Go" button, but return false will not disable it. – Tapas Pal May 11 '15 at 06:28
-
The code will work just fine. There's no public API to disable the GO button – Yariv Nissim May 11 '15 at 06:30
-
1I knew there is no public API, I only concern that your answer doesn't fulfil what actually asked . – Tapas Pal May 11 '15 at 06:38
-
ok, updated my response. saying "not possible" and providing an alternative does not make the answer wrong. – Yariv Nissim May 11 '15 at 06:41
-
Option 1 (subclassing)
You can subclass UITextField
and override hasText
property:
class CustomTextField : UITextField {
override public var hasText: Bool {
get {
return validateInput(text)
}
}
}
Where validateInput(_ string: String?) -> Bool
checks against your needed input criteria, for example character count.
Of course this only works in combination with enablesReturnKeyAutomatically = true
set on the UITextField
.
Option 2 (extending)
extension UITextFieldDelegate {
func setReturnKeyState(for textField: UITextField, isEnabled: Bool, delay: Double? = nil) {
textField.enablesReturnKeyAutomatically = false
if textField.delegate != nil {
if let delay = delay {
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
textField.setValue(isEnabled, forKeyPath: "inputDelegate.returnKeyEnabled")
}
} else {
textField.setValue(isEnabled, forKeyPath: "inputDelegate.returnKeyEnabled")
}
}
}
}
Usage
Call setReturnKeyState in delegate methods, for example:
func textFieldDidBeginEditing(_ textField: UITextField) {
setReturnKeyState(for: textField, isEnabled: validateInput(textField.text), delay: 0.1) // A bit hacky it needs delay here
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if var text = textField.text, let range = Range(range, in: text) {
text.replaceSubrange(range, with: string)
setReturnKeyState(for: textField, isEnabled: validateInput(text))
}
return true
}
Sample validateInput()
implementation:
func validateInput(_ string: String?) -> Bool {
(string?.count ?? 0) > 3
}

- 3,989
- 33
- 46
While you have to use set the return key function for the Textfeilds and use the code as demo like
if (Condition True){
id keyboard = [self magicallyGetAUIKeyboardInstance];
[keyboard setReturnKeyEnabled: YES];
}
else {
id keyboard = [self magicallyGetAUIKeyboardInstance];
[keyboard setReturnKeyEnabled: NO];
}
You only have to enable and disable the Keyboard Return key functionality

- 1,543
- 1
- 11
- 19