Skip to content

Instantly share code, notes, and snippets.

@quangtqag
Last active January 19, 2016 04:16
Show Gist options
  • Save quangtqag/bcfacf5ba1855f158476 to your computer and use it in GitHub Desktop.
Save quangtqag/bcfacf5ba1855f158476 to your computer and use it in GitHub Desktop.
Push text field up to prevent keyboard cover in table view
override func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWillShow(aNotification: NSNotification) {
// Get the size of the keyboard.
let info = aNotification.userInfo
let aValue = info?[UIKeyboardFrameEndUserInfoKey] as? NSValue
let keyboardSize = aValue?.CGRectValue().size
tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize!.height, 0)
let voteCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))
if voteCell != nil {
tableView.scrollRectToVisible(voteCell!.frame, animated: true)
}
}
func keyboardWillHide(aNotification: NSNotification) {
tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment