views:

200

answers:

1

I have multiple editable textfiels and some of them are covered with keyboard. So I used UIScrollView and it works quite nice.

Problem is when I want to hide keyboard. If I was scrolled down, after the keyboard hides, everything jumps up as it was at beginning (without keyboard). I want to tween this part as the keyboard is hiding.
So far I got this code (2 methods for keyboard events):

-(void)keyboardWillShow:(NSNotification *)notif{
    if(keyboardVisible)
        return;
    keyboardVisible = YES;

    NSDictionary* info = [notif userInfo];
    NSValue* value = [info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    CGRect viewFrame = self.view.frame;
    viewFrame.size.height -= keyboardSize.height;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3];
    [scrollView setFrame:viewFrame];
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)notif{
    if(!keyboardVisible)
        return;

    keyboardVisible = NO;
    NSDictionary* info = [notif userInfo];
    NSValue* value = [info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    CGRect viewFrame = self.view.frame;
    viewFrame.size.height += keyboardSize.height;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3];
    [scrollView setFrame:viewFrame];
    [UIView commitAnimations];
}

It works pretty well with hiding keyboard but unfortunately it doesn't work when user switches from one text field to another. It will fire keyboardWillHide and keyboardWillShow events, one right after another. This will result in two animations, second one interrupting the first one. It doesn't look good.

Problem is with keyboardWillHide firing even when keyboard will not hide. At that point I don't know if keyboard will be shown again or not.

I also tried it with UIScrollView scrollRectToVisible and setContentOffset methods.. but they resulted in glitches when keyboard was hiding.

A: 

Why not use boolean values to indicate whether it is an appearance or just changing?

jrtc27
What do you mean? I am checking if the keyboard isn't showing/hiding more than once. But at the time when keyboard is visible, if user clicks on different text field, it will trigger hide event followed by a show event... during hide event I can not find out if there is going to be a show event. That's the problem
Antriel
make the boolean 1 on `keyboardDidShow`, and 0 on `keyboardDidHide` - these are only triggered when the keyboard was not visible prior to the event.
jrtc27
problem is keyboardDidHide is triggered even when user touch different text field. But the keyboard will not hide, it will just trigger hide event and show event.
Antriel