简体   繁体   English

禁用UITextField上的触摸但不是所有用户交互

[英]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. 我有一个UITextField,但我希望能够控制它何时被聚焦以及什么时候没有聚焦。

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. 似乎没有办法用UITextField的属性来阻止触摸。 I solved this problem by placing a UIView over the text field to block the touches. 我通过在文本字段上放置UIView来阻止触摸来解决这个问题。 I set the UIView to "Clear Color" using the attributes inspector. 我使用属性检查器将UIView设置为“Clear Color”。 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 我必须在编辑时进行光标交互,所以在设置isFirstResponder()之前我设置isUserInteractionEnabled = true并且在结束编辑时将其设置为false

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


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

    textField.isUserInteractionEnabled = false

    return true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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