I'd like to add a Done button to the iPhone number pad keyboard. There's even a handy space at the bottom left for just such a button.
Previously, I was using a similar trick to those described in Question 584538 and Luzian Scherrer's excellent blog post, but that stopped working in iOS 4. I can do it by creating a custom inputView, but I'd prefer to extend Apple's keyboard instead of writing my own.
Is there a new way to add a view to the standard keyboard? Has someone published an OSS inputView for this? Is there another way?
You can add inputAccessoryView
with 'Apply' and 'Cancel' buttons, and dismiss the number pad with then.
- (void)viewDidLoad { [super viewDidLoad]; UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; numberToolbar.items = [NSArray arrayWithObjects: [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)], [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)], nil]; numberTextField.inputAccessoryView = numberToolbar; } -(void)cancelNumberPad{ [numberTextField resignFirstResponder]; numberTextField.text = @""; } -(void)doneWithNumberPad{ NSString *numberFromTheKeyboard = numberTextField.text; [numberTextField resignFirstResponder]; }
I got this working. See the code here: http://gist.github.com/454844
There were two issues:
UIKeyboardWillShowNotification
is sent before the keyboard view exists, but it you schedule your code to run on the next run loop pass, they do exist. So you don't have to bother with DidShow
which is visually less pleasing.
On iOS 4 the UIKeyboard
view was elsewhere in the view hierarchy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With