Disabling keys on keyboard

前端 未结 3 1482
栀梦
栀梦 2021-02-04 15:22

I am new to Objective-C, and I am looking to limit a user from switching from the alphabet portion of a normal keyboard to the numeric/punctuation side. This being said, I would

相关标签:
3条回答
  • 2021-02-04 15:54

    I'm not sure if the SDK has changed such that @ppaulojr's answer no longer works, or if I just have things set up weirdly on my system, but with the following tweaks I was able to get it to work!

    The posts linked in @ppaulojr's answer are great (http://www.iphonedevsdk.com/forum/iphone-sdk-development/6573-howto-customize-uikeyboard.html and http://www.iphonedevsdk.com/forum/iphone-sdk-development/6275-add-toolbar-top-keyboard.html), and they helped me to get this to work.

    Apparently the actual keyboard view is now embedded as a subview in some grander UIKeyboard view structure so a bit of recursion is involved. I got this to work:

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

    I also found that the best place to call this function is from -(void)textViewDidBeginEditing:(UITextView *)textView like so:

    - (void)textViewDidBeginEditing:(UITextView *)textView {
        NSLog(@"textViewDidBeginEditing");
    
        [self findKeyboard];
    
    }
    

    This does the keyboard modifications as soon as the keyboard is added to the window, but before it actually shows up, so that the whole time it raises from the bottom, it will have been modified.

    0 讨论(0)
  • 2021-02-04 16:00

    I implemented a neater solution. The trick is to place a disabled key image on top of the keyboard.

    To do this

    1. Run emulator (in 100% scale) and screen grab the asset you'd like (in my case, this was a disabled Done button at the bottom right end)
    2. Place this image on top of the keyboard

    Note that keyboard is placed in a separate UIWindow (since iOS5 I believe) and thus, you will need to do the following

    + (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];
    }
    
    0 讨论(0)
  • 2021-02-04 16:04

    You actually can add and remove buttons of the default UIKeyboard

    There's some recipes on the Internet like this: http://www.iphonedevsdk.com/forum/iphone-sdk-development/6573-howto-customize-uikeyboard.html and like this: http://www.iphonedevsdk.com/forum/iphone-sdk-development/6275-add-toolbar-top-keyboard.html

    Those posts show you how to add a button, however the same principle can be used to remove.

    Below I'll show you a compilation of one of the solutions:

    //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...
        }
    }
    
    0 讨论(0)
提交回复
热议问题