0

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?

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Sidharth J Dev
  • 927
  • 1
  • 6
  • 25

3 Answers3

3

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.

Yariv Nissim
  • 13,273
  • 1
  • 38
  • 44
0

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
}
Paul B
  • 3,989
  • 33
  • 46
-2

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

Fatti Khan
  • 1,543
  • 1
  • 11
  • 19