Skip to content

Instantly share code, notes, and snippets.

@loiclefloch
Created June 11, 2015 12:58
Show Gist options
  • Save loiclefloch/ad3ea44e917e4a2c72b3 to your computer and use it in GitHub Desktop.
Save loiclefloch/ad3ea44e917e4a2c72b3 to your computer and use it in GitHub Desktop.
iOS form next button handling.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)
}
override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func keyboardWasShown(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
let kbSize: CGSize = keyboardSize.size
var remaininHeight: CGFloat = self.view.height - self.scrollView.height - self.scrollView.y - 40
var contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height - remaininHeight, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
}
}
func keyboardWillBeHidden(notification: NSNotification) {
var contentInsets: UIEdgeInsets = UIEdgeInsetsZero
self.scrollView.scrollIndicatorInsets = contentInsets
self.scrollView.setContentOffset(CGPointZero, animated: true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment