diff --git a/Sources/Controllers/MessagesViewController.swift b/Sources/Controllers/MessagesViewController.swift index 7c553b9a..078c0f5c 100644 --- a/Sources/Controllers/MessagesViewController.swift +++ b/Sources/Controllers/MessagesViewController.swift @@ -60,6 +60,13 @@ open class MessagesViewController: UIViewController { /// A Boolean value used to determine if `viewDidLayoutSubviews()` has been called. private var isFirstLayout: Bool = true + + private var messageCollectionViewBottomInset: CGFloat = 0 { + didSet { + messagesCollectionView.contentInset.bottom = messageCollectionViewBottomInset + messagesCollectionView.scrollIndicatorInsets.bottom = messageCollectionViewBottomInset + } + } // MARK: - View Life Cycle @@ -84,9 +91,7 @@ open class MessagesViewController: UIViewController { defer { isFirstLayout = false } addKeyboardObservers() - messagesCollectionView.contentInset.bottom = keyboardOffsetFrame.height - messagesCollectionView.scrollIndicatorInsets.bottom = keyboardOffsetFrame.height - + messageCollectionViewBottomInset = keyboardOffsetFrame.height //Scroll to bottom at first load if scrollsToBottomOnFirstLayout { messagesCollectionView.scrollToBottom(animated: false) @@ -281,16 +286,15 @@ fileprivate extension MessagesViewController { if (keyboardEndFrame.origin.y + keyboardEndFrame.size.height) > UIScreen.main.bounds.height { // Hardware keyboard is found - let bottomInset = view.frame.size.height - keyboardEndFrame.origin.y - iPhoneXBottomInset - messagesCollectionView.contentInset.bottom = bottomInset - messagesCollectionView.scrollIndicatorInsets.bottom = bottomInset - + messageCollectionViewBottomInset = view.frame.size.height - keyboardEndFrame.origin.y - iPhoneXBottomInset } else { //Software keyboard is found - let bottomInset = keyboardEndFrame.height > keyboardOffsetFrame.height ? (keyboardEndFrame.height - iPhoneXBottomInset) : keyboardOffsetFrame.height - messagesCollectionView.contentInset.bottom = bottomInset - messagesCollectionView.scrollIndicatorInsets.bottom = bottomInset - messagesCollectionView.scrollToBottom(animated: true) + let afterBottomInset = keyboardEndFrame.height > keyboardOffsetFrame.height ? (keyboardEndFrame.height - iPhoneXBottomInset) : keyboardOffsetFrame.height + let differenceOfBottomInset = afterBottomInset - messageCollectionViewBottomInset + let contentOffset = CGPoint(x: messagesCollectionView.contentOffset.x, y: messagesCollectionView.contentOffset.y + differenceOfBottomInset) + + messageCollectionViewBottomInset = afterBottomInset + messagesCollectionView.setContentOffset(contentOffset, animated: true) } }