Skip to content

Commit

Permalink
Fixed: #18 - Top and bottom insets sometimes appear to be incorrect.
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethTsang committed Oct 31, 2017
1 parent 7f30393 commit af6a3c5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion GrowingTextView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

# This description is used to generate tags and improve search results.
Expand Down
18 changes: 12 additions & 6 deletions Pod/Classes/GrowingTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ open class GrowingTextView: UITextView {
layoutIfNeeded()
}

private var isGrowing = false
override open func layoutSubviews() {
super.layoutSubviews()

Expand All @@ -117,7 +118,9 @@ open class GrowingTextView: UITextView {
}

// Update height constraint if needed
if height != heightConstraint?.constant {
let originHeight = heightConstraint?.constant ?? 0
if height != originHeight {
if height > originHeight { isGrowing = true }
heightConstraint!.constant = height
scrollToCorrectPosition()
if let delegate = delegate as? GrowingTextViewDelegate {
Expand All @@ -127,12 +130,15 @@ open class GrowingTextView: UITextView {
}

private func scrollToCorrectPosition() {
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
if self.isFirstResponder {
self.scrollRangeToVisible(NSMakeRange(-1, 0)) // Scroll to bottom
} else {
self.scrollRangeToVisible(NSMakeRange(0, 0)) // Scroll to top
if self.isFirstResponder {
if self.isGrowing {
// Workaround to for incorrect scroll position on Swift4
self.heightConstraint!.constant += 0.0000001
self.isGrowing = false
}
self.scrollRangeToVisible(NSMakeRange(-1, 0)) // Scroll to bottom
} else {
self.scrollRangeToVisible(NSMakeRange(0, 0)) // Scroll to top
}
}

Expand Down

0 comments on commit af6a3c5

Please sign in to comment.