-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKeyboardExtension.swift
54 lines (43 loc) · 2.29 KB
/
KeyboardExtension.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// KeyboardExtension.swift
// Gap
//
// Created by Daniele on 11/04/2019.
// Copyright © 2019 nexor. All rights reserved.
//
import UIKit
protocol KeyboardProtocol {
var scrollView: UIScrollView! { get set }
}
extension KeyboardProtocol where Self: UIViewController {
func registerForKeyboardNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(sender:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(sender:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
}
fileprivate extension UIViewController {
@objc func keyboardWillShow(sender: NSNotification) {
let info = sender.userInfo!
let keyboardSize = (info[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height
let keyboardViewController = self as! KeyboardProtocol
let bottomInset = keyboardSize - (self.tabBarController?.tabBar.frame.size.height ?? 0)
if keyboardViewController.scrollView.transform.d == -1 { //keyboardViewController.scrollView.transform.d support viewcontroller rotation like for chat
keyboardViewController.scrollView.contentInset.bottom = -bottomInset
keyboardViewController.scrollView.scrollIndicatorInsets.bottom = 0
}
else {
keyboardViewController.scrollView.contentInset.bottom = bottomInset
keyboardViewController.scrollView.scrollIndicatorInsets.bottom = bottomInset
}
let duration: TimeInterval = (info[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
UIView.animate(withDuration: duration) { self.view.layoutIfNeeded() }
}
@objc func keyboardWillHide(sender: NSNotification) {
let info = sender.userInfo!
let duration: TimeInterval = (info[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
let keyboardViewController = self as! KeyboardProtocol
keyboardViewController.scrollView.contentInset.bottom = 0
keyboardViewController.scrollView.scrollIndicatorInsets.bottom = 0
UIView.animate(withDuration: duration) { self.view.layoutIfNeeded() }
}
}