how to get keyboard location in ios 8 & add DONE button on numberPad

后端 未结 3 1112
花落未央
花落未央 2020-12-06 03:18

I am using below code to get the keyboard location from view & add DONE button on it.But in ios 8 it is not able to get keyboard location & hence not add DONE butto

3条回答
  •  暖寄归人
    2020-12-06 03:50

    The below code is for showing "DONE" button on NumberPad iOS 8 also. I run this code in XCode-5.1.1 with iOS 6/7/8 devices. Its working perfectly.

    I take the refrence from this link Can't find keyplane that supports type 4 for keyboard iPhone-Portrait-NumberPad given some code for add button on Number keyboard.

    @property (nonatomic, retain) UIButton *doneButton;
    

    AddButton

    - (void)addButtonToKeyboard
    {
        if (!self.doneButton)
        {
            self.doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.doneButton addTarget:self action:@selector(doneButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
        self.doneButton.adjustsImageWhenHighlighted = NO;
        [self.doneButton setTitle:@"DONE" forState:UIControlStateNormal];
        [self.doneButton.titleLabel setFont:[UIFont systemFontOfSize:16.0]];
        [self.doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    
        // locate keyboard view
        if ([[[UIApplication sharedApplication] windows] count] <= 1) return;
        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:@"

    removeButtonFromKeyboard

    - (void)removeButtonFromKeyboard
    {
        NSArray *arTemp = [[UIApplication sharedApplication] windows];
        if ([arTemp count] <= 1) return;
        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:@"

    Let me know any issues.

    Update: Testing on iOS 7.1, real device - the button is not being added unless the keyboard show animation has finished. The code below adds a delay to add button once the keyboard is fully visible:

    -(void)textFieldDidBeginEditing:(UITextField *)textField
    {
        [self performSelector:@selector(addButtonToKeyboard) withObject:nil afterDelay:0.75];
    
    }
    

提交回复
热议问题