Created
June 11, 2015 12:58
-
-
Save loiclefloch/ad3ea44e917e4a2c72b3 to your computer and use it in GitHub Desktop.
iOS form next button handling.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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