Disable touch but not all user interaction on UITextField

I have a UITextField, but I want to control when it is focused and when it is not focused.

To achieve this, I need to be able to block touch events in this text box. I know that I can just put a button in front of it and manually do it, but I wanted to know if there is a way to disable the touch of an element.

Many thanks.

+5
source share
3 answers

There seems to be no way to block bindings using the UITextField property. I solved this problem by putting a UIView over the text box to block the touch. I set the UIView to "Clear Color" using the attribute inspector. Great for my application.

+13
source

How about a property userInteractionEnabled?

+1
source

I had to have an interaction with the cursor during editing, so before getFirstResponder () sets isUserInteractionEnabled = true, and at the end of editing set it to false

@IBAction func editTapped(_ sender: UIButton) {
    textField.isUserInteractionEnabled = true
    textField.becomeFirstResponder()
}


func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
    print(textField.textValue)

    textField.isUserInteractionEnabled = false

    return true
}
+1
source

All Articles