Forked from xaphod/gist:7aab1302004f6e933593a11ad8f5a72d
Created
November 19, 2017 09:14
-
-
Save jaunesarmiento/fdb872f12f6521e1b088a84af69dd41a to your computer and use it in GitHub Desktop.
Scroll keyboard up just enough to show control. Swift 3.
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
// 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