简体   繁体   English

当这个 textField 在 Swift 中被禁用时,点击 textField 的 rightView?

[英]Tap on rightView of textField when this textField be disabled in Swift?

I have a textField like this:我有一个这样的 textField:

@IBOutlet weak var titleTextField:UITextField!

Then I add image beside this textField:然后我在这个 textField 旁边添加图像:

titleTextField.rightView = titleImg

with titleImg is a imageView. titleImg 是一个 imageView。 I used UITapGestureRecognizer to tap on titleImg imageView, it working fine.我使用 UITapGestureRecognizer 来点击 titleImg imageView,它工作正常。

And finally, I set:最后,我设置:

titleTextField.userInteractionEnabled = false 

but with this, my titleImg can't be tap.但是有了这个,我的 titleImg 无法点击。 It mean I want disable titleTextField, but user can tap on titleImg.这意味着我想禁用 titleTextField,但用户可以点击 titleImg。 How to resolve it?如何解决?

尝试启用UIImage's用户交互。

titleImg.userInteractionEnabled = true 

Your titleImg is added to titleTextField, so on disabling user interaction of text field, it won't allow image view to take tap. 您的titleImg被添加到titleTextField,因此在禁用文本字段的用户交互时,将不允许点击图像视图。 You should remove titleImg from titleTextField and use separate UIButton with image added for taking tap while text field is disabled. 您应该从titleTextField中删除titleImg,并使用单独的UIButton,其中添加的图像可以在禁用文本字段时进行点击。

So I see this question has not had a final solution, therefore I add my solution for this.所以我看到这个问题还没有最终解决方案,因此我为此添加了我的解决方案。 You don't need to set titleTextField.userInteractionEnabled = false您不需要设置titleTextField.userInteractionEnabled = false

let it's true and set delegate for your titleTextField to your ViewController (for example) or any object you consider.让它为true并将titleTextField委托设置为您的 ViewController(例如)或您考虑的任何对象。

Implement the delegate function textFieldShouldBeginEditing and return false value and it will work.实现委托函数textFieldShouldBeginEditingreturn false值,它将起作用。

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    return false
}

Your title IMG is added to the title text field, so disabling user interaction of the text field, it will not allow the image view to be tapped.你的标题 IMG 被添加到标题文本字段,因此禁用文本字段的用户交互,它将不允许点击图像视图。 You should remove titleImg from the title text field and use a separate UIButton with the added picture to take tap when the text field is disabled.您应该从标题文本字段中删除 titleImg 并使用单独的 UIButton 和添加的图片在文本字段被禁用时进行点击。

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

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