禁用键盘上的按键

时间:2012-03-06 20:46:03

标签: ios keyboard keyboard-events

我是Objective-C的新手,我希望限制用户从普通键盘的字母部分切换到数字/标点符号。这就是说,我想禁用键盘上的按钮进行切换。也许我正在使用错误的搜索参数,但我在谷歌上找不到什么,在我的参考书中更少。我如何实际禁用iOS键盘上的按钮?使用字母/数字/标点字符,只需忽略输入的输入字符即可轻松完成,但在键盘部分之间切换的按钮执行操作而不是返回字符。

感谢您的帮助!

3 个答案:

答案 0 :(得分:4)

您实际上可以添加和删除默认UIKeyboard

的按钮

互联网上有一些食谱:http://www.iphonedevsdk.com/forum/iphone-sdk-development/6573-howto-customize-uikeyboard.html并且像这样:http://www.iphonedevsdk.com/forum/iphone-sdk-development/6275-add-toolbar-top-keyboard.html

这些帖子向您展示了如何添加按钮,但是可以使用相同的原则来删除。

下面我将向您展示其中一个解决方案的汇编:

//The UIWindow that contains the keyboard view - 
//It some situations it will be better to actually
//iterate through each window to figure out where the keyboard is, 
// but In my applications case
//I know that the second window has the keyboard so I just reference it directly
//
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] 
//                     objectAtIndex:1];

//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;

//Iterate though each view inside of the selected Window
for(int i = 0; i < [tempWindow.subviews count]; i++)
{
    //Get a reference of the current view 
    keyboard = [tempWindow.subviews objectAtIndex:i];

    //Check to see if the className of the view we have 
    //referenced is "UIKeyboard" if so then we found
    //the keyboard view that we were looking for
    if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
    {
        // Keyboard is now a UIView reference to the 
        // UIKeyboard we want. From here we can add a subview
        // to th keyboard like a new button

        //Do what ever you want to do to your keyboard here...
    }
}

答案 1 :(得分:1)

我实施了一个更整洁的解决方案。诀窍是将禁​​用的关键图像放在键盘顶部。

要做到这一点

  1. 运行模拟器(100%比例)并屏幕抓取您想要的资产(在我的情况下,这是右下角的禁用完成按钮)
  2. 将此图像放在键盘顶部
  3. 请注意,键盘放在单独的UIWindow中(因为iOS5我相信),因此,您需要执行以下操作

    + (void) addSubviewToTop:(UIView *)view {
         int count = [[[UIApplication sharedApplication] windows] count];
         if(count <= 0)  {
            warn(@"addSubviewToTop failed to access application windows");
         }
         UIWindow *top_window = [[[UIApplication sharedApplication] windows] objectAtIndex:count-1];
         [top_window addSubview:view];
         [top_window bringSubviewToFront:view];
    }
    

答案 2 :(得分:0)

我不确定SDK是否已更改,以致@ ppaulojr的答案不再有效,或者我的系统上的设置很奇怪,但通过以下调整我能够让它工作! / p>

在@ ppaulojr的答案中链接的帖子非常棒(http://www.iphonedevsdk.com/forum/iphone-sdk-development/6573-howto-customize-uikeyboard.htmlhttp://www.iphonedevsdk.com/forum/iphone-sdk-development/6275-add-toolbar-top-keyboard.html),他们帮我完成了这项工作。

显然,实际的键盘视图现在作为子视图嵌入到一些更宏大的UIKeyboard视图结构中,因此需要进行一些递归。我得到了这个工作:

-(void) findKeyboard {

    NSArray* windows = [[UIApplication sharedApplication] windows];

    for (int i = 0; i < [windows count]; i++) {

        UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] 
                                                            objectAtIndex:i];

        for(UIView *subView in [tempWindow subviews])
        {
            [self checkViews:subView];
        }
    }
}

-(void)checkViews:(UIView *)inView
{
    for(UIView *keyboard in inView.subviews)
    {
        NSLog( @"ViewName: %@", [keyboard description] );    // Which view are we looking at

        //Check to see if the className of the view we have 
        //referenced is "UIKeyboard" if so then we found
        //the keyboard view that we were looking for
        if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
        {
            // Keyboard is now a UIView reference to the 
            // UIKeyboard we want. From here we can add a subview
            // to th keyboard like a new button

            //Do what ever you want to do to your keyboard here...


            break;
        }

        // Recurse if not found
        [self checkViews:subView];
    }    
}

我还发现调用此函数的最佳位置来自-(void)textViewDidBeginEditing:(UITextView *)textView,如此:

- (void)textViewDidBeginEditing:(UITextView *)textView {
    NSLog(@"textViewDidBeginEditing");

    [self findKeyboard];

}

键盘添加到窗口后,键盘修改完成,但在实际显示之前,所以从底部升起的整个时间都会被修改。