0

When I open the application and click in the UITextField it is being instantly dismissed the first time it is clicked. It does not get dismissed after the first time without clicking outside of the view.

I used this answer: Close iOS Keyboard by touching anywhere using Swift for the code to dismiss the textField when an area outside the textField is tapped. Code:

class SettingsViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var textField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    textField.delegate = self
}


 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    view.endEditing(true)
}


func textFieldShouldReturn(_ textField: UITextField) -> Bool {

    textField.resignFirstResponder()
    return false
}
Community
  • 1
  • 1
Ryan Hampton
  • 319
  • 1
  • 4
  • 21

1 Answers1

0

I think from your question, that you want to dismiss the keyboard when someone taps on the view and not the text field. You can override touches began on your view controller to that end:

import UIKit

class ViewController: UIViewController {

    // MARK: - Properties
    @IBOutlet weak var textField: UITextField!

    // MARK: - Actions
    // will resign any first repsonder on this view contorller
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }

    // MARK: - Lifecycle
    override func viewDidLoad() {
        textField.delegate = self
    }

}

extension ViewController: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return false
    }
}

And not have to worry about taps, etc.

Fred Faust
  • 6,696
  • 4
  • 32
  • 55
  • Sorry if it wasnt clear, my problem is that the textField is being automatically dismissed the first time that it is tapped and the keyboard is opened. This problem does not seem to occur after the first tap though. – Ryan Hampton Feb 04 '17 at 13:59
  • Can you help me out with 'textField automatically dismissed' - I'm inferring that to mean that the keyboard hides? – Fred Faust Feb 04 '17 at 14:01
  • 1
    Correct, when the textField is tapped, the keyboard appears for a split second then automatically hides / get dismissed. – Ryan Hampton Feb 04 '17 at 14:05
  • I'm sure this is a duplicate - I think you are over engineering with keeping a tap state and the gesture recognizer. Remove all that and just override touches began in the answer I gave, based on what you have provided that should work for you. I'm going to try to find a duplicate to link to and vote to close this question though in the meantime. – Fred Faust Feb 04 '17 at 14:08
  • I've updated my answer to demonstrate how it should work. – Fred Faust Feb 04 '17 at 14:18
  • You are right on the over engineering front I think as when I updated to the code in my question now, the issue no longer happens. However, now the textField is not being dismissed when tapping outside of it. – Ryan Hampton Feb 04 '17 at 14:21