-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathUIViewControllerExtensions.swift
56 lines (49 loc) · 2.72 KB
/
UIViewControllerExtensions.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
55
56
import Foundation
import UIKit
extension UIViewController {
@objc func scrollViewOnKeyboardWillShow(notification: NSNotification,
scrollView: UIScrollView,
activeField: UITextField?) {
let additionalSpace = CGFloat(80.0)
let textField = activeField ?? UIResponder.current as? UITextField
guard let activeField = textField else { return }
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let contentInsets = UIEdgeInsets(top: 0.0,
left: 0.0,
bottom: keyboardSize.height + additionalSpace,
right: 0.0)
scrollView.contentInset = contentInsets
scrollView.scrollIndicatorInsets = contentInsets
// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
var aRect: CGRect = self.view.frame
aRect.size.height -= keyboardSize.height
let activeTextFieldRect: CGRect = activeField.frame
let activeTextFieldOrigin: CGPoint = activeTextFieldRect.origin
if !aRect.contains(activeTextFieldOrigin) {
scrollView.scrollRectToVisible(activeTextFieldRect, animated: true)
}
}
}
@objc func scrollViewOnKeyboardWillHide(notification: NSNotification, scrollView: UIScrollView) {
let contentInsets = UIEdgeInsets.zero
scrollView.contentInset = contentInsets
scrollView.scrollIndicatorInsets = contentInsets
}
func registerKeyboardHandlers(notificationCenter: NotificationCenter,
keyboardWillShow: Selector,
keyboardWillHide: Selector) {
notificationCenter.addObserver(self,
selector: keyboardWillShow,
name: UIResponder.keyboardWillShowNotification,
object: nil)
notificationCenter.addObserver(self,
selector: keyboardWillHide,
name: UIResponder.keyboardWillHideNotification,
object: nil)
}
func deregisterKeyboardHandlers(notificationCenter: NotificationCenter) {
notificationCenter.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
notificationCenter.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}
}