0

I am developing an app in which I want to open a view when the user clicks on a text field but I don't want to open the keyboard. How can I achieve this?

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Wings
  • 2,398
  • 23
  • 46

4 Answers4

4

You can easily achieve it by setting an empty custom input view to the textField:

textField.inputView = UIView(frame: CGRect.zero)

Then the textField can become first responder normally, but this view (with zero frame) will be presented instead of the keyboard.

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
3

Set your ViewController as your TextFields delegate using

self.yourTextfield.delegate = self

and now implement the UITextFieldDelegate as shown below

extension ViewController : UITextFieldDelegate {
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool   {
        //Load your VC here
        return false
    }
}

Thats all :) Hope it helps

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
2

You can use below textField Delegate Method

func textFieldDidBeginEditing(_ textField: UITextField) {
        yourTextField.resignFirstResponder()
        //code for show view
    }

Hope it will help!

Kaushik Makwana
  • 1,329
  • 2
  • 14
  • 24
1

You can simply add a tap gesture recognizer to the text field.

let tapGestRec = UITapGestureRecognizer(target: self, action: #selector(textFieldTapped))
textField.addGestureRecognizer(tapGestRec)
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223