EffectiveProgramming-Forks / Reference-iOS

iOS Reference
MIT License
0 stars 0 forks source link

Keyboard handler implementation skeleton #10

Open LutherBaker opened 9 years ago

LutherBaker commented 9 years ago
- (void)keyboardWillShow:(NSNotification *)notification {

    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    CGRect keyboardEndFrame;

    [[notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];

    CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:animationCurve];
    NSLog(@"frame: %@", NSStringFromCGRect(keyboardFrame));
    [UIView commitAnimations];
}

if the user has more than one keyboard installed and they switch keyboards using the international keyboard key (which only displays if you have more than one keyboard installed). This will throw a UIKeyboardWillShowNotification, even though the keyboard is already shown. The result will be that your textField will be moved up another keyboard height up the screen. This will occur every time the international keyboard key is pressed.

LutherBaker commented 9 years ago

The original idea above doesn't seem to work well with floats or doubles for the animationDuration

    NSDictionary *userInfo = notification.userInfo;
    NSTimeInterval animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve animationCurve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    CGRect keyboardEndFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];