UIKeyboard 上的自定义按钮

发布于 2024-12-03 19:23:54 字数 1224 浏览 3 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赤濁 2024-12-10 19:23:54

我认为你尝试的方式是硬编码黑客攻击和违反 HIG。

如果您想自定义键盘,您应该创建自己的键盘并将其设置到您的UIResponderinputView

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's inputView.

夏天碎花小短裙 2024-12-10 19:23:54

我想出了解决办法。在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];
        }
    }
}

}

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:

- (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];
        }
    }
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文