Skip to content

Instantly share code, notes, and snippets.

@oaleeapp
Last active May 6, 2019 02:05
Show Gist options
  • Save oaleeapp/34cdd6f77fb9f4f52082607f13d8bb18 to your computer and use it in GitHub Desktop.
Save oaleeapp/34cdd6f77fb9f4f52082607f13d8bb18 to your computer and use it in GitHub Desktop.
Keyboard Extention
class ViewController {
@IBOutlet weak var scrollView: UIScrollView!
// ...
}
extension ViewController {
func registerKeyboardNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notif:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notif:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func keyboardWillShow(notif: Notification) {
guard let frame = notif.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else {
return
}
scrollView.contentInset = UIEdgeInsets(top: 0.0,
left: 0.0,
bottom: frame.height - 100,
right: 0.0)
}
@objc func keyboardWillHide(notif _: Notification) {
scrollView.contentInset = UIEdgeInsets()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment