Skip to content

Commit af6a3c5

Browse files
committed
Fixed: #18 - Top and bottom insets sometimes appear to be incorrect.
1 parent 7f30393 commit af6a3c5

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

GrowingTextView.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = "GrowingTextView"
11-
s.version = "0.5.0"
11+
s.version = "0.5.1"
1212
s.summary = "UITextView on Swift 3 and Swift 4. Support auto growing, placeholder and length limit."
1313

1414
# This description is used to generate tags and improve search results.

Pod/Classes/GrowingTextView.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ open class GrowingTextView: UITextView {
9494
layoutIfNeeded()
9595
}
9696

97+
private var isGrowing = false
9798
override open func layoutSubviews() {
9899
super.layoutSubviews()
99100

@@ -117,7 +118,9 @@ open class GrowingTextView: UITextView {
117118
}
118119

119120
// Update height constraint if needed
120-
if height != heightConstraint?.constant {
121+
let originHeight = heightConstraint?.constant ?? 0
122+
if height != originHeight {
123+
if height > originHeight { isGrowing = true }
121124
heightConstraint!.constant = height
122125
scrollToCorrectPosition()
123126
if let delegate = delegate as? GrowingTextViewDelegate {
@@ -127,12 +130,15 @@ open class GrowingTextView: UITextView {
127130
}
128131

129132
private func scrollToCorrectPosition() {
130-
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
131-
if self.isFirstResponder {
132-
self.scrollRangeToVisible(NSMakeRange(-1, 0)) // Scroll to bottom
133-
} else {
134-
self.scrollRangeToVisible(NSMakeRange(0, 0)) // Scroll to top
133+
if self.isFirstResponder {
134+
if self.isGrowing {
135+
// Workaround to for incorrect scroll position on Swift4
136+
self.heightConstraint!.constant += 0.0000001
137+
self.isGrowing = false
135138
}
139+
self.scrollRangeToVisible(NSMakeRange(-1, 0)) // Scroll to bottom
140+
} else {
141+
self.scrollRangeToVisible(NSMakeRange(0, 0)) // Scroll to top
136142
}
137143
}
138144

0 commit comments

Comments
 (0)