问题描述
我们如何实现UIKeyboardTypeNumberPad,以便它会有一个完成按钮?默认情况下没有。
How can we implement UIKeyboardTypeNumberPad so that it will have a 'done' button? By default it does not have one.
推荐答案
如果我没有错,那么你想问如何添加一个自定义完成按钮到UIKeyboardTypeNumberPad的键盘。在这种情况下,这可能是有帮助的。声明一个UIButton * doneButton in.h并将以下代码添加到.m文件
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
}
在我的应用程序和按钮的框架相同,因此调整,因此,你可以因此调用[self addButtonToKeyboard],每当你需要在键盘上显示完成按钮。
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.
这篇关于UIKeyboardTypeNumberPad没有完成按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!