Move up the UIView containing UITextField and UIButton when Keyboard shows up not working

In the main view, I've a UITableView beneath that I've another view, textInputView that contains a UITextField and a UIButton.

Move up the UIView containing UITextField and UIButton when Keyboard shows not working.

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

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

    [UIView beginAnimations: @"animateTextField" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    textInputView.frame = CGRectOffset(textInputView.frame, 0, movement);
    [UIView commitAnimations];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{

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

If I replace the textInputView with the self.view it moves up the entire view but that I don't want. I just want to move up the textInputView.

Here is the view hierarchy:

view heirarchy

Here is my actual view:

enter image description here

By the way, I'm using XCode 6 with iOS 8 SDK...

Upvotes: 0

Views: 3643

Answers (4)

Himanshu Parashar
Himanshu Parashar

Reputation: 470

For IOS Swift

func textFieldDidBeginEditing(textField: UITextField) {
        animateTextField(textField, up: true)
    }

func textFieldDidEndEditing(textField: UITextField) {
        animateTextField(textField, up: false)
    }
func animateTextField (textField:UITextField, up:Bool) {

        var movementDistance = 80

        var movementDuration = 1.0
        var movement = CGFloat(up ? -movementDistance : movementDistance)
        UIView.beginAnimations("anim", context: nil)
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(movementDuration)
        self.view.frame = CGRectOffset(self.view.frame, 0, movement)
        UIView.commitAnimations()
    }

Upvotes: 1

Bhavesh Nayi
Bhavesh Nayi

Reputation: 3656

#define kOFFSET_FOR_KEYBOARD    50.0; //change height for textfield

- (void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    CGRect rect = self.textInputView.frame;
    rect.origin.y = 0.0;
    if (movedUp)
    {
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
    }
    if(!movedUp)
    {
        rect.origin.y = 45.0f;
    }
    self.textInputView.frame = rect;
    [UIView commitAnimations];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{

    self setViewMovedUp:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self setViewMovedUp:NO];
}

Upvotes: 3

xphobia
xphobia

Reputation: 126

1.First of all you have to set < UITextFieldDelegate > in your .h file

2.you have to set the delegate self.textfieldname.delegate = self and then

- (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 = kOFFSET_FOR_KEYBOARD; // 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 hope it helps

Upvotes: 2

Sandeep
Sandeep

Reputation: 21144

Since you are using the frame for the textInputView, it is different at different times and so you are not getting the desired effect,

Do it like so,

[self.view bringSubviewToFront: textInputView]
if(up){
  textInputView.frame = CGRectOffset(textInputView.bounds, 0 self.view.bounds.size.height - textInputView.bounds.size.height)
}else{
  textInputView.frame = CGRectOffset(textInputView.bounds, 0 self.view.bounds.size.height)
}

Now, I feel that you want to show the input view just above the keyboard when keyboard appears and hide it when keyboard disappears. The preferred way to do is by observing for UIKeyboardWillShowNotification and UIKeyboardWillHideNotification. It also provides the animation duration and final keyboard frame.

- (void)viewDidLoad{
  [super viewDidLoad];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

}


- (void)keyboardWillShow:(NSNotification*)note{
  NSDictionary *userInfo = note.userInfo;
  CGRect finalKeyboardFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  NSTimeInterval animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

  float inputViewFinalYPosition = self.view.bounds.size.height - finalKeyboardFrame.size.height;
  CGRect inputViewFrame = textInputView.bounds;
  inputViewFrame.origin.y = inputViewFinalYPosition;

  [UIView animateWithDuration:animationDuration animations:^{
    textInputView.frame = inputViewFrame;
  }];

}

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

  NSDictionary *userInfo = note.userInfo;
  NSTimeInterval animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

  CGRect inputViewFrame = textInputView.bounds;
  inputViewFrame.origin.y = self.view.bounds.size.height;

  [UIView animateWithDuration:animationDuration animations:^{
    textInputView.frame = inputViewFrame;
  }];

}

Upvotes: 5

Related Questions