Search code examples
iosswiftkeyboarduitextfieldresignfirstresponder

how to hide keyboard immediately after tapping on a text field?


I have a custom textfield. It opens a pop-up using UITableViewController, when user taps on it.

I wanted to prevent keyboard to pop-up for my text field, looks like it is not possible.

So I tried the following code which works in simulator, but it does not work for an actual iPhone!!!

    @IBAction func provincePressed(_ sender: Any) {
         (sender as AnyObject).resignFirstResponder()
         self.view.endEditing(true)

What is wrong? and how can I make it work for an actual iPhone? or possibly prevent keyboard to show up at all!


Solution

  • Try to return false in the textFieldShouldBeginEditing method

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