views:

33

answers:

1

For an app I’m designing for the iPad, I have a scroll view with some text fields/text views in it. In order to keep everything visible, I adjust the contentSize property of the scroll view to add a buffer at the bottom that corresponds to how much the keyboard overlaps the scroll view. Here’s the code (there’s some app-specific stuff here, but hopefully not so much that you can’t make sense of it):

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [nc addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc removeObserver:self name:nil object:nil];
}

- (void)keyboardWillShow:(NSNotification *)aNotification
{
    NSValue *animationCurve = [[aNotification userInfo] valueForKey:UIKeyboardAnimationCurveUserInfoKey];
    UIViewAnimationCurve curve;
    [animationCurve getValue:&curve];

    NSValue *animationDuration = [[aNotification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval duration;
    [animationDuration getValue:&duration];

    NSValue *endingFrame = [[aNotification userInfo] valueForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect frame;
    [endingFrame getValue:&frame];

    [UIView beginAnimations:@"keyboardWillShow" context:bodyView];
    [UIView setAnimationCurve:curve];
    [UIView setAnimationDuration:duration]; 

    // Re-draw code here.

    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)aNotification
{

    NSValue *animationCurve = [[aNotification userInfo] valueForKey:UIKeyboardAnimationCurveUserInfoKey];
    UIViewAnimationCurve curve;
    [animationCurve getValue:&curve];

    NSValue *animationDuration = [[aNotification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval duration;
    [animationDuration getValue:&duration];

    [UIView beginAnimations:@"keyboardWillHide" context:bodyView];
    [UIView setAnimationCurve:curve];
    [UIView setAnimationDuration:duration];

    // Re-draw code here

    [UIView commitAnimations];      
}

My question is this: what do I do about keyboard size during rotation? I don’t receive any keyboard notifications when the iPad is rotated, but the size of the keyboard changes significantly. Ideally I’d simply adjust the height of the contentSize property by the amount the keyboard overlaps in landscape mode, but I can't see a good way to do that without hard-coding the height of the keyboard in both orientations, which I don’t want to do.

A: 

I accidentally found out the answer to this debugging something else. It turns out that when the iPad rotates from portrait to landscape, the portait keyboard hides (and sends its notification) just before the landscape keyboard appears (and sends its notification). So as long as you account for that, you’re OK.

Jeff Kelley