简体   繁体   中英

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.

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.

userInteractionEnabled属性怎么样?

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM