Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
Dismissing the Keyboard5:42 with Ben Jakuben
When collecting input from the user, we must make sure that the fields don't get covered up when the soft keyboard appears. We can dismiss the keyboard or programmatically move the fields out of the way of the keyboard. In this video we'll see how to simply dismiss it.
>>[male voice over] If we run our app on the 3.5-inch screen, watch what happens when we try to log in.
We go to enter our name and—oh no, the keyboard is covering the Log In button.
This is a common problem when collecting input from the user.
The screen doesn't automatically adjust for this soft keyboard, it just covers up whatever we have below.
The solution is to dismiss the keyboard somehow and/or enable our own automatic scrolling
so that the view adjusts when the soft keyboard appears.
We're going to take a look at both solutions.
Let's start with dismissing the keyboard—so, we do this using a delegate method from UITextField.
Remember that Username and Password are UITextFields
and that when we tap on them, the soft keyboard is displayed automatically.
So, basically when the return key is pressed on that keyboard
we want to dismiss it. That way the user can then tap on the button that was hidden below the keyboard.
So, let's go to LoginViewController.h—we want the header file first,
and we're going to make it conform to the UITextFieldDelegate
You need to sign up for Treehouse in order to download course files.Sign up