Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIKeyboardTypeNumberPad return or done button in iOS 5

Tags:

iphone

ios5

In iOS 3 this workaround involved creating a customer UIButton and adding this as a subview to the Apple keyboard. When iOS 4 was released the view that the subview was added to changed. So to get this working in iOS3 & 4 I had the following.

-(void) addDoneButtonToNumericKeyboard {
if(doneButtonIsAdded) {
    return;
}
if(doneButtonForNumericKeyboard == nil) {
    doneButtonForNumericKeyboard= [[UIButton buttonWithType:UIButtonTypeCustom] retain];
    doneButtonForNumericKeyboard.frame = CGRectMake(0, 163, 106, 53);
    doneButtonForNumericKeyboard.adjustsImageWhenHighlighted = NO;
    if ([[[UIDevice currentDevice] systemVersion] hasPrefix:@"3"]) {
        [doneButtonForNumericKeyboard setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
        [doneButtonForNumericKeyboard setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
    } else {        
        [doneButtonForNumericKeyboard setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
        [doneButtonForNumericKeyboard setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
    }
    [doneButtonForNumericKeyboard addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
}
// locate keyboard view
NSArray *winArray = [[UIApplication sharedApplication] windows];
if(winArray == nil || [winArray count] < 2) {
    NSLog(@"No winArray found!");
    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 view found; add the custom button to it
    NSLog(@"Keyboard description : %@", [keyboard description]);
    if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES || [[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) {
        NSLog(@"Attaching done button");
        [keyboard addSubview:doneButtonForNumericKeyboard];
        doneButtonIsAdded = YES;
    }
}
}

Anyone have any ideas how to get this working for iOS 5?

Neither of the methods above work for 5. There is an attempt to add the subview that seems to be successful but nothing is displayed. The area is just blank on screen and does not respond to a touch.

like image 705
Simon Cruise Avatar asked Oct 14 '11 11:10

Simon Cruise


1 Answers

I noticed this as well and the only fix I was able to come up so far was to change it from: keyboardWillShow: to keyboardDidShow:. The difference between "will" and "did" is obviously another discussion but it seems that fixed it for my needs.

I hope this helps.

Some more background on what I found is that the [[UIApplication sharedApplication] windows] the first object does appear to be there but the second object, at index 1, is gone. I wonder what caused this.

Thanks.

like image 194
GTsphere Avatar answered Nov 14 '22 22:11

GTsphere