Dismissing the keyboard (the simple way)


#1

As UIViewController inherits from UIResponder, it supports touch events.

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

#2

This is a nice alternate solution. For whatever reason, I was crashing and receiving an error “textfield.resignfirstresponder() unexpectedly found nil when unwrapping optional value” and I couldn’t exactly figure out what was causing it.


#3

It is likely that textField in ViewController is not connected to the UITextField in storyboard.