Last active
May 6, 2019 02:05
-
-
Save oaleeapp/34cdd6f77fb9f4f52082607f13d8bb18 to your computer and use it in GitHub Desktop.
Keyboard Extention
This file contains hidden or 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
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