Home > Software engineering >  UITextField does not detect changes with 'swipe to type'
UITextField does not detect changes with 'swipe to type'

Time:07-15

I have a textfield with a attributed target which is supposed to detect for changes to the textfield label, and then run a function. However, the .editingChanged UIControl event is not triggered when a user swipes to type (on the keyboard); this also happens when a email or password is autofilled.

My code:

let textField: UITextField = {
    let textField = UITextField()
    textField.backgroundColor = .clear
    textField.font = UIFont.systemFont(ofSize: 14, weight: .medium)
    textField.textColor = .label
    textField.placeholder = ""
    return textField
}()
textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)

@objc func textFieldDidChange(_ textField: UITextField) {
    print(textField.text)
}

CodePudding user response:

Have you tried including textField.sendActions(for: .editingChanged)?

It's also possible that textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) might be returning false?

  • Related