Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hashaam/edd9bca34036617c7d19926265fb4c94 to your computer and use it in GitHub Desktop.
Save hashaam/edd9bca34036617c7d19926265fb4c94 to your computer and use it in GitHub Desktop.
Adjust Scroll View Content Inset Based on Keyboard Frame
// https://hashaam.com/2017/09/02/adjust-scroll-view-content-inset-based-on-keyboard-frame
func adjustForKeyboardHandler(notification: Notification) {
// this method assumes scrollView is defined
guard let userInfo = notification.userInfo else { return }
guard let value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardFrame = value.cgRectValue
var contentInset: UIEdgeInsets!
switch notification.name {
case Notification.Name.UIKeyboardWillHide:
contentInset = UIEdgeInsets(top: 64.0, left: 0.0, bottom: 0.0, right: 0.0)
case Notification.Name.UIKeyboardWillChangeFrame:
contentInset = UIEdgeInsets(top: 64.0, left: 0.0, bottom: keyboardFrame.size.height, right: 0.0)
default:
break
}
scrollView.contentInset = contentInset
scrollView.scrollIndicatorInsets = contentInset
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment