Ios Swift Override double tap of UItextField

大兔子大兔子 提交于 2019-12-08 00:25:28

The only way I know of for you to do this is to

  1. put a UIView behind the UITextField
  2. set the textField's userInteractionEnabled = false
  3. add the double tap gesture to the UIView

This will allow you to register a double tap on the TextField area and not popup any keyboard or enter editing mode.

Not sure what you plan on doing with the textField after double tap but you should be able to handle most stuff programmatically at this point.

Code for this is:

class ViewController: UIViewController {
    @IBOutlet weak var myViewBehindMyTextField: UIView!
    @IBOutlet weak var myTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.myTextFieldTapped(_:)))
        tapGesture.numberOfTapsRequired = 2
        myViewBehindMyTextField.addGestureRecognizer(tapGesture)
    }

    func myTextFieldTapped(sender: UITapGestureRecognizer) {
        print("Double tapped on textField")
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!