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

后端 未结 3 1108
花落未央
花落未央 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:52

    Okay here's a simple fix for getting 'done' button to show and work in my app in both iOS 9, iOS 8 and below when I got similar error. It could be observed after running an app and viewing it via 'View's Hierarchy' (i.e. clicking on the 'View Hierarchy' icon from Debug Area bar while app is running on device and inspecting your views in Storyboard), that the keyboard is presented on different windows in iOS 9 compared to iOS 8 and below versions and have to be accounted for. addButtonToKeyboard

    - (id)addButtonToKeyboard
    {
    if (!doneButton)
    {
       // create custom button
        UIButton * doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        doneButton.frame = CGRectMake(-2, 163, 106, 53);
        doneButton.adjustsImageWhenHighlighted = NO;
        [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
        [doneButton addTarget:self action:@selector(saveNewLead:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    NSArray *windows = [[UIApplication sharedApplication] windows];
    //Check to see if running below iOS 9,then return the second window which bears the keyboard   
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) {
    return windows[windows.count - 2];
    }
    else {
    UIWindow* keyboardWithDoneButtonWindow = [ windows lastObject];
    return keyboardWithDoneButtonWindow;
        }
    }
    

    And this is how you could removeKeyboardButton from keyboard if you want.

    - (void)removeKeyboardButton {
    
    id windowTemp = [self addButtonToKeyboard];
    
    if (windowTemp) {
    
        for (UIView *doneButton in [windowTemp subviews]) {
            if ([doneButton isKindOfClass:[UIButton class]]) {
                [doneButton setHidden:TRUE];
            }
        }
      }
    }
    

    If you using UITextfieldsDelegeates methods, you can simply call the addButtonToKeyboard method in the textFieldDidBeginEditing delegate method and that should do it. Now if you toggling back and forth between NumberPad and Default keyboard, it is recommended you call the removeKeyboardButton in "textFieldShouldEndEditing" delegate method to prevent any mishaps.

提交回复
热议问题