I’m trying to hide the number pad, but I do not want to implement a button.
Is there a way to dismiss the number pad when the user taps outside the textfield?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is one of those questions where you read it and say “That’s easy you just..”. And then you go to do it and make it super complicated. And then realize it doesn’t have to be that complicated.
The answer I’ve come up with, and I’m sure it will help someone else, Is to use an invisible
UIViewthat never interacts but acts on other views and maybe not in the way you’d think.The typical answer to a question about dismissing the
UIKeyboardTypeNumberPadkeyboard is to add a bar that has a button as theinputAccessoryViewto dismiss the keyboard. If a bar and button are undesirable generally you just listen for touch events on the background and your good to go but this question is about a tableview and that makes this much harder.But this
inputAccessoryViewfeature is still awesome. It allows you to define aUIVieworUIViewsubclass to be displayed when the keyboard is shown. More importantly when the keyboard is shown due to a textfield for which it is theinputAccessoryViewbecoming first responder.I could yammer on but first here is some code for a lightweight class that actually performs very well in testing.
The contents of
NJ_KeyboardDismisser.hare:And the contents of
NJ_KeyboardDismisser.mare:You may recognize the
endEditing:method, as mentioned by Cosique, it is a UIView extension method that asks a views nested textfield to resign. Sound handy? It is. By calling it on the tableview the textfield it contains resigns first responder. Since this technique works on allUIViews there is no need to artificially limit this outlet to onlyUITableViews so the outlet is justUIView *mainView.The final moving part here is the
UITapGestureRecognizer. We don’t want to add this recognizer full time for fear of screwing up the tableview’s workings. So we take advantage ofUIView‘s delegate methoddidMoveToWindow. We don’t really do anything with the window we just check to see if we are in one; If we are then one of our textfields is first responder, if not then it’s not. We add and remove our gesture recognizer accordingly.Okay straightforward enough, but how do you use it? Well if instantiating in code you could do it like this, in
tableView:cellForRowAtIndexPath::If you are using static cells in a storyboard then the technique is different (obviously). First drag out a generic
NSObjectand place it in the dark grey strip below the view (where the other objects such as the view controller are). Then change this new object’s class to beNJ_KeyboardDismisser. Then connect the “Keyboard Dismisser”‘smainViewproperty to that view (generally a tableview). Then connect theinputAccessoryViewproperty from any each text field in that scene you wish to the “Keyboard Dismisser”.Give it a try! The tableview acts normally. Apple’s tap recognizer is smart enough to ignore the swipes on the table, so you can scroll. It also ignores touches in the textfields so you can edit and select other textfields. But tap outside a textfield and the keyboard is gone.
Note: This class’s use is not limited to tableviews. If you want to use it on a regular view, just set the
mainViewproperty to be the same as the view controller’s view.