Skip to content

Instantly share code, notes, and snippets.

@RichFell
Last active April 12, 2016 14:02
Show Gist options
  • Save RichFell/fd389b1641c0b3a5fca9 to your computer and use it in GitHub Desktop.
Save RichFell/fd389b1641c0b3a5fca9 to your computer and use it in GitHub Desktop.
Keyboard Change Frame Notification
NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardWillChangeFrameNotification, object: nil, queue: nil) { (notification) -> Void in
guard let info: [NSObject: AnyObject] = notification.userInfo,
let keyboardFrameValue = info[UIKeyboardFrameEndUserInfoKey] as? NSValue,
let duration = info[UIKeyboardAnimationDurationUserInfoKey] as? Int else {
return
}
let diff = CGRectGetHeight(keyboardFrameValue.CGRectValue())
UIView.animateWithDuration(Double(duration), animations: { () -> Void in
self.view.frame = CGRect(x: self.view.frame.minX, y: keyboardFrameValue.CGSizeValue().height, width: self.view.frame.width, height: self.view.frame.height)
self.view.layoutIfNeeded()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment