@@ -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