0

I have two UIViews inside my UIView. Here is my view: enter image description here

On textfield focused, keyboard is shown and keyboard hides my textield. I want to move whole TextField View up before keyboard. I've found code example here on stackoverflow: iPhone Keyboard Covers UITextField

This is the code:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
  [self animateTextField: textField up: YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
  [self animateTextField: textField up: NO];
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
  const int movementDistance = 80; // tweak as needed
  const float movementDuration = 0.3f; // tweak as needed

  int movement = (up ? -movementDistance : movementDistance);

  [UIView beginAnimations: @"anim" context: nil];
  [UIView setAnimationBeginsFromCurrentState: YES];
  [UIView setAnimationDuration: movementDuration];
  self.view.frame = CGRectOffset(self.view.frame, 0, movement);
  [UIView commitAnimations];
}

I'm trying to implement this but I am having trouble with it. Can someone explain how to properly do that?

Community
  • 1
  • 1
Flipper
  • 1,107
  • 1
  • 11
  • 32
  • 1
    there are a bunch of questions regarding this problem on SOF, please google it before asking, here is the first result http://stackoverflow.com/questions/6435670/keyboard-hides-uitextfield-how-to-solve-this – Basheer_CAD Jun 15 '15 at 14:20
  • possible duplicate of [How to make a UITextField move up when keyboard is present](http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present) – Basheer_CAD Jun 15 '15 at 14:22

2 Answers2

1

Instead of doing this manually. use TPKeyboardAvoidingScrollView. Its easy to use.

First take UIScrollView and put ur all views inside it.

For use with UITableViewController classes, drop TPKeyboardAvoidingTableView.m and TPKeyboardAvoidingTableView.h into your project, and make your UITableView a TPKeyboardAvoidingTableView in the xib. If you're not using a xib with your controller, I know of no easy way to make its UITableView a custom class: The path of least resistance is to create a xib for it.

For non-UITableViewControllers, drop the TPKeyboardAvoidingScrollView.m and TPKeyboardAvoidingScrollView.h source files into your project, pop a UIScrollView into your view controller's xib, set the scroll view's class to TPKeyboardAvoidingScrollView, and put all your controls within that scroll view. You can also create it programmatically, without using a xib - just use the TPKeyboardAvoidingScrollView as your top-level view.

iAnurag
  • 9,286
  • 3
  • 31
  • 48
0

You could write it manually using something like this:

- (void)makeRoomForKeyboard
{
    UIView *rootView = [[[UIApplication sharedApplication]delegate] window].rootViewController.view.subviews[0];

    [UIView animateWithDuration:0.2f animations:^{
        rootView.center = CGPointMake(rootView.center.x, rootView.center.y - kKeyboardOffset);
    } completion:nil];
}

And play around with the kKeyboardOffset as you see fit. Call this from textFieldDidBeginEditing. When you finish editing, the same function can be called with just a + sign in front of the kKeyboardOffset.

Shaked Sayag
  • 5,712
  • 2
  • 31
  • 38