Last active
January 19, 2016 04:16
-
-
Save quangtqag/bcfacf5ba1855f158476 to your computer and use it in GitHub Desktop.
Push text field up to prevent keyboard cover in table view
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) { | |
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