1

In our company iPhone app, we are using Number Pad keyboard with a custom button hyphen "-". Throughout the iOS versions, we had to modify the code as below. Until up to iOS 10, the code below worked fine, but we are having an issue in iOS11. when i tap on the textbox on screen #1, the keypad along with "-" displays as expected. When i hit the enter button on that same screen #1, the app takes me to screen #2, but when i hit the back button on screen # 2, and tap on that same textbox in screen #1, the keypad does display again but the hyphen key does not. Hope you can help. thank you. We are also looking for a permanent solution for this so that we don't have to revisit the same issue in iOS12 etc..

- (void)addButtonToKeyboard
{

NSString *iOSVersion = [[UIDevice currentDevice] systemVersion];
float iOSF=[iOSVersion floatValue];

if(iOSF <= 7.2)
{
    doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
        [doneButton setImage:[UIImage imageNamed:@"doneup.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
    } else {
        [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
    }
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
    // locate keyboard view
    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 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
            if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
                [keyboard addSubview:doneButton];
        } else {
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
                [keyboard addSubview:doneButton];
        }
    }
}

else if (iOSF >=8.0 && iOSF <8.3 )
{

    doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0)
    {
        [doneButton setImage:[UIImage imageNamed:@"doneup.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
    }
    else
    {
        [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
    }
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
    // locate keyboard view
    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 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2)
        {
            if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
                [keyboard addSubview:doneButton];
        }
        else
        {
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
                [keyboard addSubview:doneButton];
        }

        if([[keyboard description] hasPrefix:@"<UIInputSetContainerView"] == YES)
        {
            for(int i = 0 ; i < [keyboard.subviews count] ; i++)
            {
                UIView* hostkeyboard = [keyboard.subviews objectAtIndex:i];
                if([[hostkeyboard description] hasPrefix:@"<UIInputSetHost"] == YES)
                {
                    BOOL isPortrait = UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
                    doneButton.frame = CGRectMake(((isPortrait) ? 0 : -1),((int) (hostkeyboard.frame.size.height*3)/4) + ((isPortrait) ? 0 : 1),(int) hostkeyboard.frame.size.width/3-1, (isPortrait) ? 54 : 38);
                    [hostkeyboard addSubview:doneButton];
                }
            }
        }
        else{}

    }
}

else if (iOSF >= 8.3 && iOSF <=8.5)
{

    doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    // create custom button
    doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 53, [[UIScreen mainScreen] bounds].size.width / 3, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setTag:67123];
    [doneButton setImage:[UIImage imageNamed:@"doneup.png"]        forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"donedown.png"] forState:UIControlStateHighlighted];
    //doneButton1.hidden=false;
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardHide:) name:UIKeyboardWillChangeFrameNotification object:nil];


    // locate keyboard view
    int windowCount = [[[UIApplication sharedApplication] windows] count];
    if (windowCount < 2) {
        return;
    }


    tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];


    //  doneButton = (UIButton*)[tempWindow viewWithTag:67123];
    // if (doneButton == nil)//to avoid adding again and again as per my requirement (previous and next button on keyboard)
    [tempWindow addSubview:doneButton];

}
else{
    UIButton *doneButton1 ;
    UIWindow* tempWindow1;
    doneButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
    // create custom button
    doneButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton1.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 53, [[UIScreen mainScreen] bounds].size.width / 3, 53);
    doneButton1.adjustsImageWhenHighlighted = NO;
    [doneButton1 setTag:67123];
    [doneButton1 setImage:[UIImage imageNamed:@"doneup.png"]        forState:UIControlStateNormal];
    [doneButton1 setImage:[UIImage imageNamed:@"donedown.png"] forState:UIControlStateHighlighted];
    //doneButton1.hidden=false;
    [doneButton1 addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];

    // [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardHide:) name:UIKeyboardWillChangeFrameNotification object:nil];


    // locate keyboard view
    int windowCount = [[[UIApplication sharedApplication] windows] count];
    if (windowCount < 2) {
        return;
    }


    tempWindow1 = [[[UIApplication sharedApplication] windows] objectAtIndex:2];


    //  doneButton = (UIButton*)[tempWindow viewWithTag:67123];
    // if (doneButton == nil)//to avoid adding again and again as per my requirement (previous and next button on keyboard)
    [tempWindow1 addSubview:doneButton1];}

}
//Addind button to keypad "-"
- (void)keyboardWillShow:(NSNotification *)note
{
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2)
{
    [self addButtonToKeyboard];
}
}
//Addind button to keypad "-"
- (void)keyboardDidShow:(NSNotification *)note
{
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2)
{
    [self addButtonToKeyboard];
}
}

//Adds dash button to keyboard
- (void)doneButton:(id)sender
{

@try {
    enterTxtBox.text = [enterTxtBox.text stringByAppendingString:@"-"];

}
@catch (NSException *exception) {

}
@finally {

}

}



-(void)onKeyboardHide:(NSNotification *)notification
{
//- key on keyboard will hide
   doneButton.hidden=true;
}
Robbie H.
  • 11
  • 3
  • First this is not a good idea as you shouldn't go messing about inside the default controls like the keyboard. Apple could decide to reject your app for this. You could implement your own custom keyboard instead. Secondly if you are going to do something like this there will not be a permanent solution (for iOS12, 13, 14...) because you are fooling about inside a control and Apple can change that whenever they decide to. You are already having to do different things for different versions and who's to say in iOS12 they just stop you being able to access the internals of the keyboard full stop – Upholder Of Truth Dec 28 '17 at 22:41
  • Hi Robbie H. I can suggest you one library, which already provide the keyboard management in single line of code. refer this link - https://github.com/hackiftekhar/IQKeyboardManager – Samkit Shah Dec 29 '17 at 07:02
  • @SamkitShah will I be able to customize the IQKeyboardManager to be like the number pad keyboard with the hyphen “-“ . – Robbie H. Dec 31 '17 at 16:47
  • @UpholderOfTruth thanks for your feedback on this. – Robbie H. Dec 31 '17 at 16:47
  • No problem just offering some early advice in case you go down this route and then have problems later. – Upholder Of Truth Dec 31 '17 at 16:58

0 Answers0