Skip to content

Instantly share code, notes, and snippets.

@amosavian
Last active November 1, 2016 20:33
Show Gist options
  • Save amosavian/145f798e247413744becd2c0a5b45800 to your computer and use it in GitHub Desktop.
Save amosavian/145f798e247413744becd2c0a5b45800 to your computer and use it in GitHub Desktop.
override func viewWillAppear(_ animated: Bool) {
// Note cell keybaord adjustment
NotificationCenter.default.addObserver(self, selector: #selector(adjustViewForKeyboardNotification(_:)), name: Notification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(adjustViewForKeyboardNotification(_:)), name: Notification.Name.UIKeyboardWillHide, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self)
}
func adjustViewForKeyboardNotification(_ notification: Notification) {
let notif = (notification.userInfo as NSDictionary?) as? [String: NSValue]
if let keyboardFrame = notif?[UIKeyboardFrameEndUserInfoKey]?.cgRectValue, let duration = notif?[UIKeyboardAnimationDurationUserInfoKey] as? Double {
UIView.beginAnimations(nil, context: nil);
UIView.setAnimationDuration(duration);
UIView.setAnimationBeginsFromCurrentState(true);
var tableViewFrame = tableView.frame
let finalKeyboardFrame = view.convert(keyboardFrame, from: view.window)
tableViewFrame.size.height = abs(finalKeyboardFrame.origin.y) + tableView.contentInset.bottom
tableView.frame = tableViewFrame;
tableView.scrollToRow(at: IndexPath(row: row, section: 0), at: UITableViewScrollPosition.bottom, animated: true)
UIView.commitAnimations()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment