Skip to content

Commit 9b0deb6

Browse files
committed
disabled auto scroll and fixed some frame resizing issues
1 parent d7c2529 commit 9b0deb6

File tree

1 file changed

+46
-19
lines changed

1 file changed

+46
-19
lines changed

iOSFormUtils/Form.swift

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import Foundation
1010
import SnapKit
11+
import UIKit
1112

1213
// MARK: Protocols
1314

@@ -38,6 +39,10 @@ public protocol FormDelegate {
3839
func getNextInput(_ form: Form, currentInput: TextInput) -> TextInput?
3940
}
4041

42+
public protocol FormDataSource {
43+
func getContainer() -> UIView
44+
}
45+
4146
// MARK: Class
4247
/// UIScrollView child class for forms handling
4348
open class Form: UIScrollView {
@@ -55,10 +60,14 @@ open class Form: UIScrollView {
5560
/// The stored delegate
5661
public var formDelegate: FormDelegate!
5762

63+
public var formDataSource: FormDataSource!
64+
5865
/// The current input which has been focused
5966
fileprivate var currentInput: TextInput! {
6067
didSet {
61-
minimizeScrollingZone(currentInput)
68+
if oldValue != currentInput && nil != currentInput {
69+
minimizeScrollingZone(currentInput)
70+
}
6271
}
6372
}
6473

@@ -93,7 +102,14 @@ open class Form: UIScrollView {
93102
name: NSNotification.Name.UIKeyboardDidShow,
94103
object: nil
95104
)
96-
105+
106+
NotificationCenter.default.addObserver(
107+
self,
108+
selector: #selector(Form.keyboardHidden(_:)),
109+
name: NSNotification.Name.UIKeyboardDidHide,
110+
object: nil
111+
)
112+
97113
NotificationCenter.default.addObserver(
98114
self,
99115
selector: #selector(Form.textFieldReturnedFired(_:)),
@@ -114,9 +130,9 @@ open class Form: UIScrollView {
114130
name: NSNotification.Name(rawValue: tfBecameFirstResponderNotifName),
115131
object: nil
116132
)
117-
if let _ = formDelegate {
118-
currentInput = formDelegate.getFirstInput(self)
119-
}
133+
// if let _ = formDelegate {
134+
// currentInput = formDelegate.getFirstInput(self)
135+
// }
120136
}
121137

122138
public func reloadData() {
@@ -154,28 +170,31 @@ open class Form: UIScrollView {
154170
fileprivate func minimizeScrollingZone(_ input: TextInput) {
155171
if (!viewScrolledForKeyboard) {
156172
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+
}
161178
}
162179

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+
// }
169186
}
170187

171188
/**
172189
Resets the scrolling zone to its original value.
173190
*/
174191
open func resetScrollingZone() {
175192
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+
}
179198
}
180199

181200
// MARK: NSNotification listeners
@@ -221,8 +240,16 @@ open class Form: UIScrollView {
221240

222241
let rawFrame = value.cgRectValue
223242
let keyboardFrame = self.convert(rawFrame!, from: nil)
224-
243+
225244
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()
226253
}
227254

228255
/**

0 commit comments

Comments
 (0)