自定义iPhone键盘

vic*_*irk 8 iphone user-interface iphone-softkeyboard

我需要(即客户要求)为用户提供自定义键盘,以便在文本字段和区域中键入文本.我已经有了键盘的功能并将测试附加到文本字段,但是我想让它更通用并使其像标准的iphone键盘一样,即当用户选择可编辑的文本控件时出现.目前我的控制器知道目标,目标是不可编辑的,以防止标准键盘.

有没有办法挂钩文本控件的行为,所以我很容易使用自己的键盘?

谢谢,维克

luv*_*ere 11

这是一个想法:根据您自己的需要修改现有键盘.首先,注册以在屏幕上显示时收到通知:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                      selector:@selector(modifyKeyboard:)
                                      name:UIKeyboardWillShowNotification
                                      object:nil];
Run Code Online (Sandbox Code Playgroud)

然后,在您的modifyKeyboard方法中:

- (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];
            }
}
Run Code Online (Sandbox Code Playgroud)

这会将您的视图添加到原始键盘的顶部,因此请确保将其设置为不透明.


dwe*_*ery 10

您可以使用iOS 3.2或更高版本.检查UITextField的inputView属性以获取详细信息.


ian*_*anh 7

只要您没有将应用程序提交到应用程序商店,就可以使用称为方法调配的技术在运行时动态替换核心类的方法.例如:

@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
Run Code Online (Sandbox Code Playgroud)

不要在任何生产代码中使用这样的内容.另外,我实际上没有测试过这个,所以YMMV.