Kakuweb Blog Rants and raves. Some interesting stuff, most of it not.

Best Way to Close the iOS Keyboard

I’m sure you’ve seen lots of different ways of closing the keyboard in iOS. One of the most popular ones seems to be to resign the first responder. And that one’s fine. But the one I like the most and I think is the simplest is to just force an end editing like so:

view.endEditing(true)

Put it into a function and call it from multiple places in your ViewController:

func closeKeyboard() {
    view.endEditing(true)
}

Maybe call it after a tap anywhere in your main view:

let tap = UITapGestureRecognizer(target: self, action: "closeKeyboard")
view.addGestureRecognizer(tap)
Twitter Share this post if you want, or follow me on Twitter if you're into that stuff.
comments powered by Disqus