Is there any notification that gets sent when the Keyboard changes (like from NumberPad to Default)

北战南征 提交于 2019-12-06 09:32:11

Got this figured out. Took a little logic, but it works flawlessly. Here is what I did: Added private properties for:

@property (nonatomic) UIKeyboardType currentKBType;
@property(nonatomic,strong) UITextField *curTextField;
@property(nonatomic,strong) UIButton *doneButton;
@property(nonatomic) BOOL doneButtonDisplayed;

Then added the following logic in the both the TextField delegate method:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    self.currentKBType = textField.keyboardType;
    if (textField.keyboardType == 4 || textField.keyboardType == 5) {
        if (!doneButtonDisplayed) {
            [self addButtonToKeyboard];
        }
    } else {
        if (doneButtonDisplayed) {
            [self removeButtonFromKeyboard];
        }
    }

    self.curTextField = textField;
    return YES;
}

And in the KeyboardDidShowNotification that I signed the VC up for in the viewDidLoad:

- (void)keyboardDidShow:(NSNotification *)note {
    if (self.currentKBType == 4 || self.currentKBType == 5) {
        if (!doneButtonDisplayed) {
            [self addButtonToKeyboard];
        }
    } else {
        if (doneButtonDisplayed) {
            [self removeButtonFromKeyboard];
        }
    }
}

And the two methods referenced in these methods:

- (void)addButtonToKeyboard {

    // create custom button
    doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;

    [doneButton setImage:[UIImage imageNamed:@"DoneNormal.png"] forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"DoneHL.png"] forState:UIControlStateHighlighted];

    [doneButton addTarget:self action:@selector(resignKeyboard) forControlEvents:UIControlEventTouchUpInside];
    // locate keyboard view
    if ([[[UIApplication sharedApplication] windows] count] > 1) {
        UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
        UIView* keyboard;
        for(int i=0; i<[tempWindow.subviews count]; i++) {
            keyboard = [tempWindow.subviews objectAtIndex:i];
            // keyboard found, add the button
            if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) {
                [keyboard addSubview:doneButton];
                self.doneButtonDisplayed = YES;
            }
        }

    }
}

- (void)removeButtonFromKeyboard {  
    [doneButton removeFromSuperview];
    self.doneButtonDisplayed = NO;
}

And finally, the resignKeyboard method that is called whenever the Done button is touched:

-(void)resignKeyboard {
    [self.curTextField resignFirstResponder];
    self.doneButtonDisplayed = NO;
}

This will add that Done button whenever a NumberPad or PhonePad type keyboard is displayed and remove it (only when it has been added) from the other keyboard types.

Tested and works great.

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