UIKeyboard 上的自定义按钮
我正在尝试在 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)
返回“否”。我已经使用 UIKeyboardDidShow 通知对此进行了测试,因此在调用代码时键盘绝对可见。
I am trying to add a custom done button on the UIKeyboard (the number pad specifically). I have followed all the examples to do this online but I can't get the button to appear above the keyboard. Below is my code (mainly patched together from online examples):
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];
}
Am I doing something wrong?
(This code is called after the keyboard is displayed).
EDIT:
I am showing the keypad in a modal view controller which is the child of a UINavigationController. In all the sample code I've tried the problem seems to be with this line:
if([[keyboard description] hasPrefix:@"<UIKeyboardTypeNumberPad"] == YES)
which is returning NO. I have tested this using the UIKeyboardDidShow notification so the keyboard is definitely visible when the code is called.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你尝试的方式是硬编码黑客攻击和违反 HIG。
如果您想自定义键盘,您应该创建自己的键盘并将其设置到您的
UIResponder
的inputView
。The way you are trying is hard-code hacking and violating HIG I think.
If you want to customize keyboard, you should create your own keyboard and set it to your
UIResponder
'sinputView
.我想出了解决办法。在iOS4.0中,Apple更改了键盘的描述,因此您现在必须使用UIPeripheralHostView(如果Apple可以随时更改该值,这显然不是实现此解决方案的最佳方法)。我对按钮的定位也关闭了,因为我将 y 坐标定位在整个视图上,而不仅仅是键盘上。这是最终的代码:
}
I figured out the solution. In iOS4.0 Apple changed the description of the keyboard so you now have to use UIPeripheralHostView (this is clearly not the best way to implement this solution if Apple can change that value at any time). My positioning of the button was also off as I was positioning the y coordinate against the whole view instead of just the keyboard. Here is the final code:
}