Say you are using a UITextField, you want to tap another place to hide the keyboard. This article provides two methods to implement the simple functionality.

First, let’s realize it mainly through IB:

image

image

Make it an action, add only one line of code as follow:

image

All done, so easy, right?

image

Then, let’s realize it through pure code:

You just need to add two piece of code:

Add image in viewDidLoad:

and add a method image

The effect is exactly the same as before. While there are of course other ways to hide keyboard, but that’s not the point in this article.

IB or pure code, which one do you prefer?

The demo project is here.