Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MARK: - UIKeyBoard
- extension Your_Controller {
- func registerKeyboardNotifications() {
- NotificationCenter.default.addObserver(self,
- selector: #selector(keyboardWillShow(notification:)),
- name: NSNotification.Name.UIKeyboardWillShow,
- object: nil)
- NotificationCenter.default.addObserver(self,
- selector: #selector(keyboardWillHide(notification:)),
- name: NSNotification.Name.UIKeyboardWillHide,
- object: nil)
- }
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- NotificationCenter.default.removeObserver(self)
- }
- @objc func keyboardWillShow(notification: NSNotification) {
- let userInfo: NSDictionary = notification.userInfo! as NSDictionary
- let keyboardInfo = userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue
- let keyboardSize = keyboardInfo.cgRectValue.size
- let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height + 30, right: 0)
- UI?.scrollView.contentInset = contentInsets
- UI?.scrollView.scrollIndicatorInsets = contentInsets
- }
- @objc func keyboardWillHide(notification: NSNotification) {
- UI?.scrollView.contentInset = .zero
- UI?.scrollView.scrollIndicatorInsets = .zero
- }
- }
Add Comment
Please, Sign In to add comment