Hide / Close keyboard after typing in uitextfield
Alright so in this video we are going to learn how to close this keyboard for the textfield 3 different ways. So the first one is a you press the button and the keyboard closes. Second one is a you press anywhere in the area and it closes. Third one is you press the return button and the keyboard closes. If you want to download the final x code project file then fill out the form here or just continue watching.
Alright so I am going to create new x code project file. iOS single view application next. I am going to call it closing keyboard. Then I am just going to click next. And save this file on the desktop.
Alright the first thing I am going to go to the main storyboard. Set the iPhone size to iPhone SE. And I am simply going to add here textfield. So drag a textfield here. Resize it to any size you want. Instead of textfield then I am going to add a button. I am going to call it save. Not going to save anything. But normally when you save something then you want to empty out the textfield.
Alright I am going to create outlet for this so I am going to open the assistant editor. Right click and drag. And I am going to call it textfield. And create an outlet to the view controller. Then I am going to create action for the save button. Set it to action. Make the type UIButton. Call it save pressed. And then connect.
I am going to close the assistant editor. Open the view controller swift file. The only thing I am going to do here is textfield dot resign first responder. And that’s it. Now I am going to run this app in iPhone 5s.
Alright so when I start editing here. Let’s say I write something. And then I press save it closes. But what if you had multiple text field in this view. So for example let’s just say. I am going to add another textfield here. And then I am going to run this on iPhone 5s.
Alright so when I type here and then I can close with save. When I type here I cannot when I press this. This doesn’t closes. So that’s why we are going to look at second method for closing keyboard.
Simply go to view controller. Then here call it touches began. And here view dot end editing true. I am going to run on iPhone 5s. Now if I start editing here and then I press anywhere on this blank area then it closes and both the text field closes. So whichever the textfield that is in editing mode will automatically close. So where as in here this was button is specifically for single textfield.
Now lets look at the third method where it closes when you press on the return button. So I am going to add UITextFieldDelegate. Then in the main storyboard I am going to add assistant editor. I mean there are two ways you can do this. One way is either right click here and connect the delegate here.
Or what I am going to do is I am going to create a outlet for both the text field. So this is new text field. Now we have outlet for both the textfield. I am going to go to view controller swift file. Make some spaces. And in view did load I am going to set textfield dot delegate equal to self. And similarly new text field dot delegate equal to self.
So this you can do either in main storyboard or programatically. I am doing it programatically. Now this delegate has this one method so if I press textfield should return so in here I am going to say textfield dot resign first responder. And I am going to say return true. So make sure see this I know there is duplicate in name so this is also text field.
And when you run this method there is text field parameter here. So this parameter I am resigning first responder. I am not closing the this single textfield. So whichever the textfield is active that will get closed when you press the return button. So now I run this app on iPhone 5s.
Now if I click on first textfield and I press return it closes. Second textfield if I return it closes. So that’s it for this video. If you like this video then click the like button on my blog if you dislike this video click the dislike button on my blog. Leave a comment on my blog. And if you want to download the X code file then just fill out the form and I’ll send you a file to your email inbox.