为 iPhone 创建自定义 UIKeyBoard

发布于 2024-08-21 23:20:46 字数 161 浏览 4 评论 0原文

如果有人有 GymBuddy 应用程序,那么他们就会知道我在说什么。他们似乎使用普通的数字键盘,但添加了一个“。”左下角的按钮以及顶部的栏可切换到字母字符。有谁知道该怎么做?我是否创建一个像键盘一样的新视图并将其拉起并使按钮对应于用于输入的文本字段?我似乎找不到任何有关自定义键盘或创建自己的键盘的信息。谢谢

If anyone has the app GymBuddy, then they will know what I am talking about. They seem to use the stock Number Pad keyboard but have added a "." button in the lower left as well as a bar across the top to switch to alpha characters. Does anyone know how to do this? Do I make a new view like the keyboard and pull it up and have the buttons correspond to the textField for input? I can't seem to find any information on customizing a keyboard or creating your own. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

獨角戲 2024-08-28 23:20:46

我已经做到了。基本上,您将自己的按钮添加为 UIKeyboard 的子视图,如下所示:

// This function is called each time the keyboard is going to be shown
- (void)keyboardWillShow:(NSNotification *)note {

// Just used to reference windows of our application while we iterate though them
UIWindow* tempWindow;

// Because we cant get access to the UIKeyboard throught the SDK we will just use UIView. 
// UIKeyboard is a subclass of UIView anyways
UIView* keyboard;

// Check each window in our application
for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; c ++)
{
    // Get a reference of the current window
    tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c];

    // Loop through all views in the current window
    for(int i = 0; i < [tempWindow.subviews count]; i++)
    {
        // Get a reference to the current view
        keyboard = [tempWindow.subviews objectAtIndex:i];

        // From all the apps i have made, they keyboard view description always starts with <UIKeyboard so I did the following
        if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
        {
            // Only add the Decimal Button if the Keyboard showing is a number pad. (Set Manually through a BOOL)
            if (numberPadShowing && [keyboard viewWithTag:123] == nil) {

                // Set the Button Type.
                dot = [UIButton buttonWithType:UIButtonTypeCustom];

                // Position the button - I found these numbers align fine (0, 0 = top left of keyboard)
                dot.frame = CGRectMake(0, 163, 106, 53);
                dot.tag = 123;

                // Add images to our button so that it looks just like a native UI Element.
                [dot setImage:[UIImage imageNamed:@"dotNormal.png"] forState:UIControlStateNormal];
                [dot setImage:[UIImage imageNamed:@"dotHighlighted.png"] forState:UIControlStateHighlighted];

                //Add the button to the keyboard
                [keyboard addSubview:dot];

                // When the decimal button is pressed, we send a message to ourself (the AppDelegate) which will then post a notification that will then append a decimal in the UITextField in the Appropriate View Controller.
                [dot addTarget:self action:@selector(sendDecimal:)  forControlEvents:UIControlEventTouchUpInside];

                return;
            }
            else if (numberPadShowing && [keyboard viewWithTag:123])
            {
                [keyboard bringSubviewToFront:dot];
            }
            else if (!numberPadShowing)
            {

                for (UIView *v in [keyboard subviews]){
                    if ([v tag]==123)
                        [v removeFromSuperview];
                }
            }
        }
    }
}
}

 - (void)sendDecimal:(id)sender {
// The decimal was pressed

}

希望这一点很清楚。

-奥斯卡

I have done this. Basically you add your own button as a subview of the UIKeyboard like this:

// This function is called each time the keyboard is going to be shown
- (void)keyboardWillShow:(NSNotification *)note {

// Just used to reference windows of our application while we iterate though them
UIWindow* tempWindow;

// Because we cant get access to the UIKeyboard throught the SDK we will just use UIView. 
// UIKeyboard is a subclass of UIView anyways
UIView* keyboard;

// Check each window in our application
for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; c ++)
{
    // Get a reference of the current window
    tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c];

    // Loop through all views in the current window
    for(int i = 0; i < [tempWindow.subviews count]; i++)
    {
        // Get a reference to the current view
        keyboard = [tempWindow.subviews objectAtIndex:i];

        // From all the apps i have made, they keyboard view description always starts with <UIKeyboard so I did the following
        if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
        {
            // Only add the Decimal Button if the Keyboard showing is a number pad. (Set Manually through a BOOL)
            if (numberPadShowing && [keyboard viewWithTag:123] == nil) {

                // Set the Button Type.
                dot = [UIButton buttonWithType:UIButtonTypeCustom];

                // Position the button - I found these numbers align fine (0, 0 = top left of keyboard)
                dot.frame = CGRectMake(0, 163, 106, 53);
                dot.tag = 123;

                // Add images to our button so that it looks just like a native UI Element.
                [dot setImage:[UIImage imageNamed:@"dotNormal.png"] forState:UIControlStateNormal];
                [dot setImage:[UIImage imageNamed:@"dotHighlighted.png"] forState:UIControlStateHighlighted];

                //Add the button to the keyboard
                [keyboard addSubview:dot];

                // When the decimal button is pressed, we send a message to ourself (the AppDelegate) which will then post a notification that will then append a decimal in the UITextField in the Appropriate View Controller.
                [dot addTarget:self action:@selector(sendDecimal:)  forControlEvents:UIControlEventTouchUpInside];

                return;
            }
            else if (numberPadShowing && [keyboard viewWithTag:123])
            {
                [keyboard bringSubviewToFront:dot];
            }
            else if (!numberPadShowing)
            {

                for (UIView *v in [keyboard subviews]){
                    if ([v tag]==123)
                        [v removeFromSuperview];
                }
            }
        }
    }
}
}

 - (void)sendDecimal:(id)sender {
// The decimal was pressed

}

Hope that's clear.

-Oscar

笑饮青盏花 2024-08-28 23:20:46

检查这篇文章,这可能是你的答案:

UIKeyboardTypeNumberPad 和缺少的“返回”键

Check this post, this could be your answer:

UIKeyboardTypeNumberPad and the missing "return" key

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