Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaunesarmiento/fdb872f12f6521e1b088a84af69dd41a to your computer and use it in GitHub Desktop.
Save jaunesarmiento/fdb872f12f6521e1b088a84af69dd41a to your computer and use it in GitHub Desktop.
Scroll keyboard up just enough to show control. Swift 3.
// note that currentFirstResponder is obtained by this:
//
// static var currentFirstResponder: UIResponder?
// func findFirstResponder(sender: Any) {
// currentFirstResponder = self
// }
//
// class func currentFirstResponder() -> UIResponder? {
// currentFirstResponder = nil
// UIApplication.shared.sendAction(#selector(findFirstResponder), to: nil, from: nil, for: nil)
// return currentFirstResponder
// }
func keyboardWillShow(notification: Notification) {
if var keyboardRect = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
keyboardRect = self.view.convert(keyboardRect, from: nil)
let keyboardCurve = (notification.userInfo?[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber)?.intValue
let keyboardDuration = (notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue
if let responder = UIResponder.currentFirstResponder() as? UIView {
let scrollViewKeyboardIntersection = self.tableView.frame.intersection(keyboardRect)
let newContentInsets = UIEdgeInsetsMake(0, 0, scrollViewKeyboardIntersection.size.height, 0)
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(keyboardDuration!)
UIView.setAnimationCurve(UIViewAnimationCurve.init(rawValue: keyboardCurve!)!)
self.tableView.contentInset = newContentInsets
self.tableView.scrollIndicatorInsets = newContentInsets
var controlFrameInScrollView = self.tableView.convert(responder.bounds, from: responder)
controlFrameInScrollView = controlFrameInScrollView.insetBy(dx: 0, dy: -10.0)
let controlVisualOffsetToTopOfScrollview = controlFrameInScrollView.origin.y - self.tableView.contentOffset.y
let controlVisualBottom = controlVisualOffsetToTopOfScrollview + controlFrameInScrollView.size.height
// visible part of scrollview not hidden by keyboard:
let scrollViewVisibleHeight = self.tableView.frame.size.height - scrollViewKeyboardIntersection.size.height
if controlVisualBottom > scrollViewVisibleHeight { // keyboard will heide control in question
var newContentOffset = self.tableView.contentOffset
newContentOffset.y += controlVisualBottom - scrollViewVisibleHeight
newContentOffset.y = min(newContentOffset.y, self.tableView.contentSize.height - scrollViewVisibleHeight)
self.tableView.setContentOffset(newContentOffset, animated: false)
} else if controlFrameInScrollView.origin.y < self.tableView.contentOffset.y {
// control not fully visible
var newContentOffset = self.tableView.contentOffset
newContentOffset.y = controlFrameInScrollView.origin.y
self.tableView.setContentOffset(newContentOffset, animated: false)
}
UIView.commitAnimations()
}
}
}
func keyboardWillHide(notification: Notification) {
self.tableView.contentInset = UIEdgeInsets.zero
self.tableView.scrollIndicatorInsets = UIEdgeInsets.zero
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment