8
8
9
9
import Foundation
10
10
import SnapKit
11
+ import UIKit
11
12
12
13
// MARK: Protocols
13
14
@@ -38,6 +39,10 @@ public protocol FormDelegate {
38
39
func getNextInput( _ form: Form , currentInput: TextInput ) -> TextInput ?
39
40
}
40
41
42
+ public protocol FormDataSource {
43
+ func getContainer( ) -> UIView
44
+ }
45
+
41
46
// MARK: Class
42
47
/// UIScrollView child class for forms handling
43
48
open class Form : UIScrollView {
@@ -55,10 +60,14 @@ open class Form: UIScrollView {
55
60
/// The stored delegate
56
61
public var formDelegate : FormDelegate !
57
62
63
+ public var formDataSource : FormDataSource !
64
+
58
65
/// The current input which has been focused
59
66
fileprivate var currentInput : TextInput ! {
60
67
didSet {
61
- minimizeScrollingZone ( currentInput)
68
+ if oldValue != currentInput && nil != currentInput {
69
+ minimizeScrollingZone ( currentInput)
70
+ }
62
71
}
63
72
}
64
73
@@ -93,7 +102,14 @@ open class Form: UIScrollView {
93
102
name: NSNotification . Name. UIKeyboardDidShow,
94
103
object: nil
95
104
)
96
-
105
+
106
+ NotificationCenter . default. addObserver (
107
+ self ,
108
+ selector: #selector( Form . keyboardHidden ( _: ) ) ,
109
+ name: NSNotification . Name. UIKeyboardDidHide,
110
+ object: nil
111
+ )
112
+
97
113
NotificationCenter . default. addObserver (
98
114
self ,
99
115
selector: #selector( Form . textFieldReturnedFired ( _: ) ) ,
@@ -114,9 +130,9 @@ open class Form: UIScrollView {
114
130
name: NSNotification . Name ( rawValue: tfBecameFirstResponderNotifName) ,
115
131
object: nil
116
132
)
117
- if let _ = formDelegate {
118
- currentInput = formDelegate. getFirstInput ( self )
119
- }
133
+ // if let _ = formDelegate {
134
+ // currentInput = formDelegate.getFirstInput(self)
135
+ // }
120
136
}
121
137
122
138
public func reloadData( ) {
@@ -154,28 +170,31 @@ open class Form: UIScrollView {
154
170
fileprivate func minimizeScrollingZone( _ input: TextInput ) {
155
171
if ( !viewScrolledForKeyboard) {
156
172
viewScrolledForKeyboard = true
157
- self . snp. updateConstraints ( { ( maker) in
158
- maker. bottom. equalTo ( self . superview!. snp. bottom) . offset ( - keyboardViewHeight)
159
- } )
160
- self . layoutIfNeeded ( )
173
+ if let _ = formDataSource {
174
+ self . snp. updateConstraints ( { ( maker) in
175
+ maker. bottom. equalTo ( formDataSource!. getContainer ( ) . snp. bottom) . offset ( - keyboardViewHeight)
176
+ } )
177
+ }
161
178
}
162
179
163
- let offSetToScroll = input. frame. origin. y - self . frame. height/ 2 + input. frame. height/ 2
164
- if 0 < offSetToScroll {
165
- UIView . animate ( withDuration: 0.2 , animations: {
166
- self . contentOffset = CGPoint ( x: 0 , y: min ( offSetToScroll, self . contentSize. height - self . frame. height + input. frame. height/ 2 ) )
167
- } )
168
- }
180
+ // let offSetToScroll = input.frame.origin.y - self.frame.height/2 + input.frame.height/2
181
+ // if 0 < offSetToScroll {
182
+ // UIView.animate(withDuration: 0.2, animations: {
183
+ // self.contentOffset = CGPoint(x: 0, y: min(offSetToScroll, self.contentSize.height - self.frame.height + input.frame.height/2))
184
+ // })
185
+ // }
169
186
}
170
187
171
188
/**
172
189
Resets the scrolling zone to its original value.
173
190
*/
174
191
open func resetScrollingZone( ) {
175
192
viewScrolledForKeyboard = false
176
- self . snp. updateConstraints ( { ( maker) in
177
- maker. bottom. equalTo ( self . superview!. snp. bottom)
178
- } )
193
+ if let _ = formDataSource {
194
+ self . snp. updateConstraints ( { ( maker) in
195
+ maker. bottom. equalTo ( formDataSource!. getContainer ( ) . snp. bottom)
196
+ } )
197
+ }
179
198
}
180
199
181
200
// MARK: NSNotification listeners
@@ -221,8 +240,16 @@ open class Form: UIScrollView {
221
240
222
241
let rawFrame = value. cgRectValue
223
242
let keyboardFrame = self . convert ( rawFrame!, from: nil )
224
-
243
+
225
244
keyboardViewHeight = keyboardFrame. height
245
+
246
+ if let accessory = self . inputAccessoryView {
247
+ keyboardViewHeight += accessory. frame. height
248
+ }
249
+ }
250
+
251
+ func keyboardHidden( _ notification: Notification ) {
252
+ resetScrollingZone ( )
226
253
}
227
254
228
255
/**
0 commit comments