Skip to content

Instantly share code, notes, and snippets.

@jingzhehu
Created August 23, 2018 20:36
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 jingzhehu/cccd695dcafa94425d6c879aa304651c to your computer and use it in GitHub Desktop.
Save jingzhehu/cccd695dcafa94425d6c879aa304651c to your computer and use it in GitHub Desktop.
ScrollView unhide content with keyboard
@IBOutlet weak var scrollView: UIScrollView!
func registerForKeyboardNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: .UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: .UIKeyboardWillHide, object: nil)
}
@IBAction func keyboardWasShown(_ notification: NSNotification) {
guard let userInfo = notification.userInfo,
let keyboardFrameValue = userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue
else {return }
let keyboardHeight = keyboardFrameValue.cgRectValue.size.height
let contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight+50, 0.0)
scrollView.contentInset = contentInsets
scrollView.scrollIndicatorInsets = contentInsets
}
@IBAction func keyboardWillBeHidden(_ notification: NSNotification) {
let contentInsets = UIEdgeInsets.zero
scrollView.contentInset = contentInsets
scrollView.scrollIndicatorInsets = contentInsets
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment