Custom iPhone Keyboard

我的梦境 提交于 2019-11-26 20:17:53

Here's an idea: modify the existing keyboard to your own needs. First, register to be notified when it appears on screen:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                      selector:@selector(modifyKeyboard:)
                                      name:UIKeyboardWillShowNotification
                                      object:nil];

Then, in your modifyKeyboard method:

- (void)modifyKeyboard:(NSNotification *)notification 
{
    UIView *firstResponder = [[[UIApplication sharedApplication] keyWindow] performSelector:@selector(firstResponder)];

    for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows])
        for (UIView *keyboard in [keyboardWindow subviews])
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
            {
                MyFancyKeyboardView *customKeyboard = [[MyFancyKeyboardView alloc] initWithFrame: CGRectMake(0, 0, keyboard.frame.size.width, keyboard.frame.size.height);
                [keyboard addSubview: customKeyboard];
                [customKeyboard release];
            }
}

This adds your view on top of the original keyboard, so make sure you make it opaque.

You can do it with iOS 3.2 or later. Check the inputView property of UITextField for details.

As long as you're not submitting your app to the app store, you can use a technique called method swizzling to dynamically replace methods of core classes at runtime. For example:

@interface UIControl(CustomKeyboard)
- (BOOL)__my__becomeFirstResponder
@end

@implementation UIControl(CustomKeyboard)
- (BOOL)__my__becomeFirstResponder
{
    BOOL becameFirstResponder = [self __my__becomeFirstResponder];
    if ([self canBecomeFirstResponder]) {
        [MyKeyboard orderFront];
    }
    return becameFirstResponder;
}

+ (void)initialize
{
    Method old = class_getInstanceMethod(self, @selector(becomeFirstResponder));
    Method new = class_getInstanceMethod(self, @selector(__my__becomeFirstResponder));
    method_exchangeImplementations(old, new);
}
@end

Please don't use anything like this in any production code. Also, I haven't actually tested this, so YMMV.

It depends how custom you want it. I personally wanted a numeric only keyboard, but didn't like the number pad, so I used the standard keyboard and added in a standard filter, which is part of the options, which rejects all the key presses that aren't numbers or space or delete.

I can't say whether Apple would like this though, and you're going to have a very hard time writing your own behavior that acts like the other keyboards. So much so, that it should be declared a bad idea.

update based on your comment it sounds more like you just need to create a view with a lot of buttons on it, and move this view around with the animate option turned on. It could then sort of slide up from the bottom like a keyboard and slide away again when dismissed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!