Use setContentOffset instead of scrollToBottom

This commit is contained in:
KEN-chan
2017-11-09 10:52:35 +09:00
parent 906036be8b
commit e272775502
@@ -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)
}
}