简体   繁体   中英

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 :

查看层次结构

Here is my actual view:

在此处输入图片说明

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

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;
  }];

}
#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];
}

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

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()
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM