-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSwiftScroll.swift
140 lines (124 loc) · 5.12 KB
/
SwiftScroll.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// SwiftScroll
//
// Created by Shubham Naik on 10/06/17.
// Copyright © 2017 Shubham. All rights reserved.
//
import UIKit
class SwiftScroll: NSObject {
static var swiftScroll: SwiftScroll?
var rootVC: UIViewController?
var reset = CGPoint.zero
var scrollView: UIScrollView?
var currentViewController: UIViewController?
public class func sharedInstance() -> SwiftScroll
{
if(swiftScroll == nil)
{
swiftScroll = SwiftScroll()
let appDelegate = UIApplication.shared.delegate
swiftScroll?.rootVC = appDelegate?.window??.rootViewController
}
return swiftScroll!
}
// MARK: - ScrollView Delegates
func thisKeyboardWasShown(_ notification: Notification, mainScroll: UIScrollView, VC: UIViewController, TXFRAME: CGRect) {
reset = mainScroll.contentOffset
let orientation = UIApplication.shared.statusBarOrientation
var keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as! CGRect).size
if orientation == .landscapeLeft || orientation == .landscapeRight {
let origKeySize = keyboardSize
keyboardSize.height = origKeySize.width
keyboardSize.width = origKeySize.height
}
let info = notification.userInfo
let kbSize = (info?[UIKeyboardFrameBeginUserInfoKey] as! CGRect).size
let deviceSize = UIScreen.main.bounds.size
var keyBdHeight: CGFloat
if kbSize.height < kbSize.width {
keyBdHeight = CGFloat(kbSize.height)
}
else {
keyBdHeight = CGFloat(kbSize.width)
}
let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(00.0, 0.0, keyBdHeight, 0.0)
mainScroll.contentInset = contentInsets
mainScroll.scrollIndicatorInsets = contentInsets
var aRect = VC.view.frame
aRect.size.height -= keyBdHeight
var nwFrm = TXFRAME
nwFrm.origin.y = TXFRAME.origin.y + TXFRAME.size.height + 5
if !aRect.contains(nwFrm.origin) {
let scrollPoint = CGPoint(x: CGFloat(0.0), y: CGFloat(abs(nwFrm.origin.y - (deviceSize.height - keyBdHeight))))
mainScroll.setContentOffset(scrollPoint, animated: true)
}
}
// Called when the UIKeyboardWillHideNotification is sent
func thisKeyboardWillBeHidden(_ aNotification: Notification, mainScroll: UIScrollView) {
let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
mainScroll.contentInset = contentInsets
mainScroll.scrollIndicatorInsets = contentInsets
}
//method to move the view up/down whenever the keyboard is shown/dismissed
func convert(_ view: UIView) -> CGRect {
var vw: UIView? = view
var rect = vw!.frame
while (vw?.superview != nil) {
vw = vw?.superview
rect.origin.x += (vw?.frame.origin.x)!
rect.origin.y += (vw?.frame.origin.y)!
}
return rect
}
func register(forNotification currentVC: UIViewController) {
// register for keyboard notifications
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWasShown(_ notification: Notification) {
let k: Any? = currentViewController?.view.currentFirstResponder()
if (k is UITextField) || (k is UITextView) {
let textview = k as! UIView
let TXFRAME = convert(textview)
thisKeyboardWasShown(notification, mainScroll: scrollView!, VC: currentViewController!, TXFRAME: TXFRAME)
}
}
func keyboardWillBeHidden(_ notification: Notification) {
thisKeyboardWillBeHidden(notification, mainScroll: scrollView!)
}
public func makeScrollable(_ scrollView: UIScrollView) {
currentViewController = findTopVC()
self.scrollView = scrollView
register(forNotification: currentViewController!)
}
func findTopVC() -> UIViewController {
var top: UIViewController? = nil
if rootVC?.navigationController != nil {
top = rootVC?.navigationController?.topViewController
}
else if rootVC?.tabBarController != nil {
top = rootVC?.tabBarController?.selectedViewController
}
else {
var parentViewController: UIViewController? = rootVC
while parentViewController?.presentedViewController != nil {
parentViewController = parentViewController?.presentedViewController
}
top = parentViewController
}
return top!
}
}
extension UIView {
func currentFirstResponder() -> UIResponder? {
if self.isFirstResponder {
return self
}
for view in self.subviews {
if let responder = view.currentFirstResponder() {
return responder
}
}
return nil
}
}