Skip to content

Stick the offset when height changes #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions MessageViewController/MessageAutocompleteController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public final class MessageAutocompleteController: MessageTextViewListener {
name: .UIKeyboardWillChangeFrame,
object: nil
)

preserveTypingAttributes(for: textView)
}

// MARK: Public API
Expand Down
6 changes: 5 additions & 1 deletion MessageViewController/MessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public final class MessageView: UIView, MessageTextViewListener {
internal var leftButtonInset: CGFloat = 0
internal var rightButtonInset: CGFloat = 0
internal var ignoreLineHeight = false
internal var suppressKVO = false

public enum ButtonPosition {
case left
Expand Down Expand Up @@ -308,7 +309,10 @@ public final class MessageView: UIView, MessageTextViewListener {
width: safeBounds.width - leftButtonMaxX - rightButtonSize.width - rightButtonInset,
height: textViewHeight
)

suppressKVO = true
textView.frame = textViewFrame
suppressKVO = false

// adjust by bottom offset so content is flush w/ text view
let rightButtonFrame = CGRect(
Expand All @@ -328,7 +332,7 @@ public final class MessageView: UIView, MessageTextViewListener {
}

public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == UITextViewContentSizeKeyPath {
if suppressKVO == false, keyPath == UITextViewContentSizeKeyPath {
textViewContentSizeDidChange()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension MessageViewController: MessageViewDelegate {

internal func sizeDidChange(messageView: MessageView) {
UIView.animate(withDuration: 0.25) {
self.layout()
self.layout(updateOffset: true)
}
}

Expand Down
12 changes: 11 additions & 1 deletion MessageViewController/MessageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ open class MessageViewController: UIViewController, MessageAutocompleteControlle
}
}

internal func layout() {
internal func layout(updateOffset: Bool = false) {
guard let scrollView = self.scrollView else { return }

let bounds = view.bounds
Expand All @@ -126,6 +126,9 @@ open class MessageViewController: UIViewController, MessageAutocompleteControlle
// required for the nested UITextView to layout its internals correctly
messageView.layoutIfNeeded()

let originalOffset = scrollView.contentOffset
let heightChange = scrollView.frame.height - messageViewFrame.minY

let frame = scrollView.frame
scrollView.frame = CGRect(
x: frame.minX,
Expand All @@ -134,6 +137,13 @@ open class MessageViewController: UIViewController, MessageAutocompleteControlle
height: messageViewFrame.minY - frame.minY
)

if updateOffset, heightChange != 0 {
scrollView.contentOffset = CGPoint(
x: originalOffset.x,
y: max(originalOffset.y + heightChange, -scrollView.util_adjustedContentInset.top)
)
}

messageAutocompleteController.layout(in: view, bottomY: messageViewFrame.minY)

didLayout()
Expand Down