Skip to content

Instantly share code, notes, and snippets.

@pietrorea
Created January 25, 2013 19:08
Show Gist options
  • Save pietrorea/4636954 to your computer and use it in GitHub Desktop.
Save pietrorea/4636954 to your computer and use it in GitHub Desktop.
How to move a UIView above the keyboard when the keyboard comes on screen. The method keyboardWillShow handles the notification UIKeyboardWillShowNotification.
- (void)keyboardWillShow:(NSNotification *)notification {
CGRect keyboardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect convertedFrame = [self.view convertRect:keyboardFrame fromView:self.view.window];
CGFloat animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve animationCurve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
[UIView animateWithDuration:animationDuration delay:0 options:animationCurve animations:^{
self.viewToMove.bottom = self.view.height - convertedFrame.size.height - 15;
} completion:nil];
}
@pietrorea
Copy link
Author

Note that "bottom" and "height" part of a UIView category. Makes it easier to access the properties of the UIView's frame.

@ettore
Copy link

ettore commented Jul 23, 2014

Hi Pietro, this is useful, thanks. Question: what's the -15 magic number for?

@pietrorea
Copy link
Author

Oh, that's left over from the app I wrote it for. Good catch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment