为iPhone创建自定义UIKeyBoard

时间:2010-02-16 19:28:16

标签: iphone objective-c uikeyboard

如果有人有应用程序GymBuddy,那么他们就会知道我在说什么。他们似乎使用股票号码垫键盘但添加了“。”左下角的按钮以及顶部的一个栏可切换到字母字符。有谁知道如何做到这一点?我是否像键盘一样制作新视图并将其拉出来并使按钮对应于textField以进行输入?我似乎无法找到有关自定义键盘或创建自己的键盘的任何信息。谢谢

2 个答案:

答案 0 :(得分:5)

我做到了这一点。基本上你添加自己的按钮作为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

}

希望很清楚。

-Oscar

答案 1 :(得分:3)

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

UIKeyboardTypeNumberPad and the missing "return" key