我正在尝试在UIKeyboard上添加自定义完成按钮(具体为数字键盘)。我已经按照所有示例在线执行此操作,但我无法将按钮显示在键盘上方。下面是我的代码(主要是从在线示例中拼凑而成):
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(214, 427, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
[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([[keyboard description] hasPrefix:@"<UIKeyboardTypeNumberPad"] == YES)
[keyboard addSubview:doneButton];
}
我做错了吗?
(显示键盘后调用此代码)。
编辑:
我在模态视图控制器中显示键盘,该控制器是UINavigationController的子级。在我试过的所有示例代码中,问题似乎都出现在这一行:
if([[keyboard description] hasPrefix:@"<UIKeyboardTypeNumberPad"] == YES)
返回NO。我已经使用UIKeyboardDidShow通知对此进行了测试,因此在调用代码时键盘肯定是可见的。
答案 0 :(得分:0)
你的尝试方式是硬编码黑客攻击和违反HIG我认为。
如果您想自定义键盘,则应创建自己的键盘并将其设置为UIResponder
的{{1}}。
答案 1 :(得分:0)
我找到了解决方案。在iOS4.0中,Apple更改了键盘的描述,因此您现在必须使用UIPeripheralHostView(如果Apple可以随时更改该值,这显然不是实现此解决方案的最佳方式)。我对按钮的定位也是关闭的,因为我将y坐标对准整个视图而不仅仅是键盘。这是最终的代码:
- (void)keyboardDidShow:(NSNotification *)note {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(215, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
[doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
UIWindow* tempWindow;
UIView* keyboard;
for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; c ++)
{
tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c];
for(int i = 0; i < [tempWindow.subviews count]; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
if([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES)
{
[keyboard addSubview:doneButton];
}
}
}
}