Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>UIKit posts <code>UIKeyboardWillShowNotification</code> when it shows the keyboard, and <code>UIKeyboardWillHideNotification</code> when it hides the keyboard. These notifications contain everything you need to properly animate your <code>UITextField</code>.</p> <p>Let's say your <code>UITextField</code> is in a property called called <code>myTextField</code>.</p> <p>First, you need to register for the notifications somewhere. Where you register depends on what object is responsible for moving <code>myTextField</code>. In my project, the field's superview is responsible, and since I load my UI from a nib, I do it in the superview's <code>awakeFromNib</code>:</p> <pre><code>- (void)awakeFromNib { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillShowNotification object:nil]; } </code></pre> <p>If you use a <code>UIViewController</code> to move the field around, you'll probably want to do it in <code>viewWillAppear:animated:</code>.</p> <p>You should unregister in your <code>dealloc</code> or <code>viewWillDisappear:animated:</code>:</p> <pre><code>- (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } </code></pre> <p>Of course the tricky bit is in the <code>keyboardWillHideOrShow:</code> method. First I extract the animation parameters from the notification:</p> <pre><code>- (void)keyboardWillHideOrShow:(NSNotification *)note { NSDictionary *userInfo = note.userInfo; NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]; CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; </code></pre> <p>The <code>keyboardFrame</code> is in the global coordinate system. I need to convert the frame to the same coordinate system as <code>myTextField.frame</code>, and <code>myTextField.frame</code> is in the coordinate system of <code>myTextField.superview</code>:</p> <pre><code> CGRect keyboardFrameForTextField = [self.myTextField.superview convertRect:keyboardFrame fromView:nil]; </code></pre> <p>Next, I compute the frame that I want <code>myTextField</code> to move to. The bottom edge of the new frame should be equal to the top edge of the keyboard's frame:</p> <pre><code> CGRect newTextFieldFrame = self.myTextField.frame; newTextFieldFrame.origin.y = keyboardFrameForTextField.origin.y - newTextFieldFrame.size.height; </code></pre> <p>Finally, I animate <code>myTextField</code> to its new frame, using the same animation parameters that the keyboard is using:</p> <pre><code> [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{ self.myTextField.frame = newTextFieldFrame; } completion:nil]; } </code></pre> <p>Here it is all put together:</p> <pre><code>- (void)keyboardWillHideOrShow:(NSNotification *)note { NSDictionary *userInfo = note.userInfo; NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]; CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGRect keyboardFrameForTextField = [self.myTextField.superview convertRect:keyboardFrame fromView:nil]; CGRect newTextFieldFrame = self.myTextField.frame; newTextFieldFrame.origin.y = keyboardFrameForTextField.origin.y - newTextFieldFrame.size.height; [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{ self.myTextField.frame = newTextFieldFrame; } completion:nil]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload