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.
Disable touches but not all user interaction on UITextField
-
06-03-2022 - |
質問
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.
解決
他のヒント
How about the userInteractionEnabled
property?
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
}
所属していません StackOverflow