0

I have a textField like this:

@IBOutlet weak var titleTextField:UITextField!

Then I add image beside this textField:

titleTextField.rightView = titleImg

with titleImg is a imageView. I used UITapGestureRecognizer to tap on titleImg imageView, it working fine.

And finally, I set:

titleTextField.userInteractionEnabled = false 

but with this, my titleImg can't be tap. It mean I want disable titleTextField, but user can tap on titleImg. How to resolve it?

rome 웃
  • 1,821
  • 3
  • 15
  • 33

4 Answers4

3

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

let it's true and set delegate for your titleTextField to your ViewController (for example) or any object you consider.

Implement the delegate function textFieldShouldBeginEditing and return false value and it will work.

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    return false
}
apieceofcode1801
  • 171
  • 1
  • 1
  • 9
0

Your titleImg is added to titleTextField, so on disabling user interaction of text field, it won't allow image view to take tap. You should remove titleImg from titleTextField and use separate UIButton with image added for taking tap while text field is disabled.

Maverick
  • 3,209
  • 1
  • 34
  • 40
0

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. 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.

Rahul
  • 1
  • 2
-1

Try enabling UIImage's user interaction.

titleImg.userInteractionEnabled = true 
iAnurag
  • 9,286
  • 3
  • 31
  • 48