简体   繁体   中英

Animating UITextField up when keyboard shows

I've registered two observers, keyboardWillShow: and keyboardWillHide: in NSNotificationCenter

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)

In both these methods when I try to animate the UIView containing the UITextView, it jumps to the frame I am trying to animate to and then animates back to the original location. I tried this same animation code in the touchesBegan method to test it out and it works great there (animating to the new frame I actually set it to rather than jumping and animating back to the original location).

func keyboardWillShow(notification: NSNotification) {
    if let endFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
        UIView.animateWithDuration(1.0, animations: { () -> Void in
            self.footerView.center = CGPointMake(self.footerView.center.x, self.footerView.center.y - endFrame.size.height)
        })
    }
}

I'm stumped. Does the keyboard notification affect new animations in some way?

You will have to call these two methods to animate the view up and down..

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
@try
{
    if (textField == (UITextField *)[self.view viewWithTag:TEXTFIELD_MOBILE_TAG])
    {
        CGRect selfframe = CGRectMake([self.view viewWithTag:VIEW_PARENT_TAG].frame.origin.x, [self.view viewWithTag:VIEW_PARENT_TAG].frame.origin.y - 80, [self.view viewWithTag:VIEW_PARENT_TAG].frame.size.width, [self.view viewWithTag:VIEW_PARENT_TAG].frame.size.height);
        [UIView animateWithDuration:0.3 animations:^{ [self.view viewWithTag:VIEW_PARENT_TAG].frame = selfframe;}];
        [textField becomeFirstResponder];
    }

}
@catch (NSException *exception)
{
    NSLog(@"Error Occured in SignUpViewController :: textViewDidBeginEditing :: %@",[exception description]);
}

}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
@try
{
    if (textField == (UITextField *)[self.view viewWithTag:TEXTFIELD_MOBILE_TAG])
    {
        CGRect selfframe = CGRectMake([self.view viewWithTag:VIEW_PARENT_TAG].frame.origin.x, [self.view viewWithTag:VIEW_PARENT_TAG].frame.origin.y + 80, [self.view viewWithTag:VIEW_PARENT_TAG].frame.size.width, [self.view viewWithTag:VIEW_PARENT_TAG].frame.size.height);
        [UIView animateWithDuration:0.3 animations:^{ [self.view viewWithTag:VIEW_PARENT_TAG].frame = selfframe;}];
        [textField resignFirstResponder];
    }
}
@catch (NSException *exception)
{
    NSLog(@"error Occured in SignUpViewController :: textViewDidEndEditing :: %@",[exception description]);
}

}

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