Image by _snapp via Flickr
Let me continue with the easy stuff to fill up the Blog a little more.Today I'll go through how to make the keyboard move out of the way once you are done editing the field.
There are two basic ways of doing it. One, is pressing the DONE key on the keyboard. The other way is tapping with your finger anywhere else on the screen.
Lets clarify:
UITextField *myTextField;
UITextField *myOtherTextField;
-(IBAction)backgroundClick:(id)sender;
-(IBAction)doneEditing:(id)sender;
Using the DONE key:
To make it return with the Done key start by adding the following code:
-(IBAction)doneEditing:(id)sender
{
[sender resignFirstResponder];
}
Any controller can be called to resign the First Responder status. With this simple function done lets set WHEN it will be called.
Open Interface Builder and single click on one of the Text Fields and click ⌘2 to open up the connections inspector.
Connect the Did End on Exit event with the doneEditing function by dragging the little circle symbol next to the event name to the File's Owner.
Repeat this for the other text field to show that you don't need a separate function for every field and try it out.
By tapping it away:
Some keyboard layouts don't have a DONE key. Or even if they have, a lot of people prefere to just tapp it away instead of tapping the small button in the corner. For these situations there is a quick and easy way of solving the problem.
Add the following code:
-(IBAction)backgroundClick:(id)sender
{
[myTextField resignFirstResponder];
[myOtherTextField resignFirstResponder];
}
Can you already guess? Lets see.
Save it again and go back to to Interface Builder.
Drag a Round Rect Button over your view and resize it so that it covers ALL of your view.
Now Select Layout > Send to Back and don't deselect the button anymore. The Button is now behind everything else you are showing.
Press ⌘1 and change the type to Custom.
You now have an invisible button in you back layer. Only thing missing now is to connect the Did touch up inside event to the action backgroundClick and you're done.
Notice that you can ask all of your fields to resign First Responder even if they don't currently have it and it won't cause any problem.
This makes ist easier, I suppose. No need for a button to hide the keyboard, when clicked anywhere else...
ReplyDelete- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[nameoftextfield resignFirstResponder];
}
Where do I put this code?
DeleteThanks man - this was super helpful :)
ReplyDeleteI'm just starting my first iPhone app and this tutorial worked perfectly, thank you so much =)
ReplyDeletePaul / 90Nz0
www.90Nz0.com
Great help, had seen the theory for this in the first book I read but couldn't remember the whole technique. Helped me avoid hours of head scratching!
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteI have a text field and a table beneath it. If I touch the table, I want the text field to resign first responder. Your method above is elegant, and I tried, it with no success. I suspect that the table is grabbing the touch event and not letting it reach the button.
ReplyDeleteI'm achieving my desired behavior by subclassing UITableView and overriding:
- (UIView *) hitTest:(CGPoint) withEvent:(UIEvent *)event;
good man..
ReplyDeletei want to move my textfield upward in the view when user wants to edit the field so user can see what is typed... because in my app the field is hide behind the keyboard...
ReplyDeleteHey, I've never written any sort of code, but the fact that I can't close my keyboard whenever I want makes me want to learn.
ReplyDeleteAssuming I'm not an idiot (and that's assuming a lot these days), where to I enter this code, what do I need to download, etc ...
Thanks for any help, and have a nice day.
Works like a charm. Thank you very much !
ReplyDeleteThanks so much for a precise tutorial, I've found so many out there that lead nowhere or that weren't complete!
ReplyDelete