2

I would to resize a UITableView and slide up a UIView that contains a UITextField when this field is fired. These are two simple mockups:

enter image description here enter image description here

Now I have this code:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationBeginsFromCurrentState:YES];

    [myView setFrame:CGRectMake(myView.frame.origin.x, myView.frame.origin.y - 167, myView.frame.size.width, myView.frame.size.height)]; // 216 (keyboard's height) - 49 (tabbar's height) = 167

    [UIView commitAnimations];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationBeginsFromCurrentState:YES];

    [myView setFrame:CGRectMake(myView.frame.origin.x, myView.frame.origin.y + 167, myView.frame.size.width, myView.frame.size.height)];

    [UIView commitAnimations];
    return TRUE;
}

The problem is that the keyboard slide up animation and the myView slide up animation are not synchronous. How to make these two animation perfectly synchronous?

And how to resize the UITableView when the keyboard is visible and return to the original height when keyboard will hide?

Fred Collins
  • 5,004
  • 14
  • 62
  • 111
  • possible duplicate of [Keeping object on top of keyboard in the event of becomeFirstResponder or resignFirstResponder?](http://stackoverflow.com/questions/8704137/keeping-object-on-top-of-keyboard-in-the-event-of-becomefirstresponder-or-resign) – rob mayoff Feb 05 '12 at 19:25
  • How about using the UIView you display on the bottom as a tableFooterView. If all of this now is wrapped into a UITableViewController, you get the functionality you want for free. – Till Feb 05 '12 at 19:55
  • @till no because I want the UIView is always visible. – Fred Collins Feb 05 '12 at 20:35
  • @FredCollins good point, that would not work then. Actually, a section footer would work for such cases, wouldn't it? – Till Feb 06 '12 at 08:20
  • Probably not Till. Btw I solved my problem in a nice way. Checkout the answer. – Fred Collins Feb 06 '12 at 22:35

1 Answers1

3

The short answer is that you need to subscribe to UIKeyboardWillShowNotification and UIKeyboardWillHideNotification. Those notifications contain the exact parameters of the keyboard animation.

The long answer is https://stackoverflow.com/a/8704371/77567.

Regarding your tab bar: the answer I linked assumes you want to slide your view down to the bottom edge of the screen when the keyboard is dismissed. Since you want to slide it down to the edge of the tab bar, you need to look at whether the keyboard is hiding or showing (by checking note.name). If it's showing, you should save the current frame of the view in an instance variable. If it's hiding, you should set the view's new frame to that frame you saved in an instance variable, instead of setting it based on the keyboard's end frame.

Community
  • 1
  • 1
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Thanks for you answer. The code works well when the keyboard will show and the field slide up, but it doesn't when the keyboard will hide and the field slide down. In a specific way, when the field slide down, it slides at `origin.y = 363` and it will hide by the `UITabBar` (with 49 height). How exactly does the `CGRect keyboardFrameForTextField = [self.commentView.superview convertRect:keyboardFrame fromView:nil];`? Do you know how to fix with the tabbar? – Fred Collins Feb 05 '12 at 20:34