How can we implement UIKeyboardTypeNumberPad so that it will have a 'done' button? By default it does not have one.
-
What do you mean by implementing keyboard, You want to hide keyboard or any thing elss. – Ishu Jan 28 '11 at 03:55
-
No... Just that there will not be a done button in the keyboard – Abhinav Jan 28 '11 at 03:57
-
1Dear number pad not having any Done button.so your question is not valid. – Ishu Jan 28 '11 at 04:35
4 Answers
If I am not wrong then You want to ask as to how to add a custom "Done" button to keyboard for UIKeyboardTypeNumberPad . In that case this might be helpful. Declare a UIButton *doneButton in.h and add the following code to .m file
- (void)addButtonToKeyboard {
// create custom button
if (doneButton == nil) {
doneButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 163, 106, 53)];
}
else {
[doneButton setHidden:NO];
}
[doneButton addTarget:self action:@selector(doneButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard = nil;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
[keyboard addSubview:doneButton];
} else {
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
}
- (void)doneButtonClicked:(id)Sender {
//Write your code whatever you want to do on done button tap
//Removing keyboard or something else
}
I am using the same in my application and the button's frame are thus adjusted, You can thus call [self addButtonToKeyboard ] whenever you need to show up the done button over the keyboard. UIKeyboardTypeNumberPad has no Done button otherwise.

- 3,013
- 1
- 20
- 47
-
I have a problem with the line UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; i get emty array with no subviews – user784625 Jan 30 '12 at 12:46
-
Where exactly do you get a blank array. In case of a keyboard being present on screen you will get atleast one item in array for sure i.e the keyboard. – Rahul Sharma Jan 30 '12 at 13:37
-
I have added the call to this function once in keyboardwilllshow and keyboarddidshow but I still get empty array – user784625 Jan 30 '12 at 13:44
-
try to make a call at some other point when You make your textfield or Textview as first responder. – Rahul Sharma Jan 31 '12 at 12:25
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
//Call this method
- (void)keyboardWillShow:(NSNotification *)note {
UIButton *doneButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 163, 106, 53)];
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:@"Done.png"] forState:UIControlStateNormal];
[doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
[keyboard addSubview:doneButton];
}
else {
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
}
}

- 3,208
- 5
- 28
- 51
iOS 5 issue resolved. Thanks a million.
I had a problem with displaying the Done button.
Replaced:
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
which did not display the done button in iOS 5...
With:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
[keyboard addSubview:doneButton];
} else {
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
Worked a treat. You're a star. Thanks, Nicola ;-)

- 142,182
- 29
- 220
- 220

- 11
- 1
If anybody had a problem that the DONE button keeps popping up when you try to load other types of keyboards in the same app, I know a lot of apps out there have this issue. Anyway, here is how I solved this: In your ViewController ( the same viewcontroller that you added DONE button ) add this code ( as is ) and it should solve your problems with DONE button keep reappearing. Hope it helps to some folks.
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
} else {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
}

- 2,070
- 2
- 18
- 18