Skip to content

Instantly share code, notes, and snippets.

@pingzh
Last active October 18, 2015 19:23
Show Gist options
  • Save pingzh/77d13eb7d57552cf9e3c to your computer and use it in GitHub Desktop.
Save pingzh/77d13eb7d57552cf9e3c to your computer and use it in GitHub Desktop.
A simple solution is to move view up with constant of keyboard height.
override func 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);
}
func keyboardWillShow(sender: NSNotification) {
let frame = (sender.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
self.view.frame.origin.y -= frame.height
}
func keyboardWillHide(sender: NSNotification) {
let frame = (sender.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
self.view.frame.origin.y += frame.height
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment