Thomas Clayson
Thomas Clayson

Reputation: 29925

Disable touches but not all user interaction on UITextField

I have a UITextField but I want to be in control of when it is focussed and when it is unfocussed.

In order to achieve this I need to be able to block touch events on that text field. I know I could just put a button in front of it and manually do it, but I wanted to know if there was any way to just disable touches on the element.

Many thanks.

Upvotes: 9

Views: 5126

Answers (3)

Markicevic
Markicevic

Reputation: 1045

I had to have cursor interaction while editing, so before becomeFirstResponder() i set isUserInteractionEnabled = true and on end editing set it on false

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


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

    textField.isUserInteractionEnabled = false

    return true
}

Upvotes: 2

T.J.
T.J.

Reputation: 3960

There does not appear to be a way to block touches with a property of UITextField. I solved this problem by placing a UIView over the text field to block the touches. I set the UIView to "Clear Color" using the attributes inspector. Works great for my application.

Upvotes: 17

borrrden
borrrden

Reputation: 33421

How about the userInteractionEnabled property?

Upvotes: 2

Related Questions