mirror of
https://github.com/MessageKit/MessageKit.git
synced 2026-02-06 19:03:19 +00:00
15e26f7c85
* Optimize collectionView scroll behavior when abruptly stops scrolling * Add CHANGELOG entry
107 lines
5.0 KiB
Swift
107 lines
5.0 KiB
Swift
/*
|
|
MIT License
|
|
|
|
Copyright (c) 2017-2018 MessageKit
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
*/
|
|
|
|
import Foundation
|
|
|
|
extension MessagesViewController {
|
|
|
|
// MARK: - Register / Unregister Observers
|
|
|
|
func addKeyboardObservers() {
|
|
NotificationCenter.default.addObserver(self, selector: #selector(MessagesViewController.handleKeyboardDidChangeState(_:)), name: .UIKeyboardWillChangeFrame, object: nil)
|
|
NotificationCenter.default.addObserver(self, selector: #selector(MessagesViewController.handleTextViewDidBeginEditing(_:)), name: .UITextViewTextDidBeginEditing, object: nil)
|
|
NotificationCenter.default.addObserver(self, selector: #selector(MessagesViewController.adjustScrollViewInset), name: .UIDeviceOrientationDidChange, object: nil)
|
|
}
|
|
|
|
func removeKeyboardObservers() {
|
|
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillChangeFrame, object: nil)
|
|
NotificationCenter.default.removeObserver(self, name: .UITextViewTextDidBeginEditing, object: nil)
|
|
NotificationCenter.default.removeObserver(self, name: .UIDeviceOrientationDidChange, object: nil)
|
|
}
|
|
|
|
// MARK: - Notification Handlers
|
|
|
|
@objc
|
|
private func handleTextViewDidBeginEditing(_ notification: Notification) {
|
|
if scrollsToBottomOnKeybordBeginsEditing {
|
|
guard let inputTextView = notification.object as? InputTextView, inputTextView === messageInputBar.inputTextView else { return }
|
|
messagesCollectionView.scrollToBottom(animated: true)
|
|
}
|
|
}
|
|
|
|
@objc
|
|
private func handleKeyboardDidChangeState(_ notification: Notification) {
|
|
guard let keyboardEndFrame = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? CGRect else { return }
|
|
|
|
if (keyboardEndFrame.origin.y + keyboardEndFrame.size.height) > UIScreen.main.bounds.height {
|
|
// Hardware keyboard is found
|
|
messageCollectionViewBottomInset = view.frame.size.height - keyboardEndFrame.origin.y - iPhoneXBottomInset
|
|
} else {
|
|
//Software keyboard is found
|
|
let afterBottomInset = keyboardEndFrame.height > keyboardOffsetFrame.height ? (keyboardEndFrame.height - iPhoneXBottomInset) : keyboardOffsetFrame.height
|
|
let differenceOfBottomInset = afterBottomInset - messageCollectionViewBottomInset
|
|
|
|
if maintainPositionOnKeyboardFrameChanged && differenceOfBottomInset != 0 {
|
|
let contentOffset = CGPoint(x: messagesCollectionView.contentOffset.x, y: messagesCollectionView.contentOffset.y + differenceOfBottomInset)
|
|
messagesCollectionView.setContentOffset(contentOffset, animated: false)
|
|
}
|
|
|
|
messageCollectionViewBottomInset = afterBottomInset
|
|
}
|
|
}
|
|
|
|
@objc
|
|
func adjustScrollViewInset() {
|
|
if #available(iOS 11.0, *) {
|
|
// No need to add to the top contentInset
|
|
} else {
|
|
let navigationBarInset = navigationController?.navigationBar.frame.height ?? 0
|
|
let statusBarInset: CGFloat = UIApplication.shared.isStatusBarHidden ? 0 : 20
|
|
let topInset = navigationBarInset + statusBarInset
|
|
messagesCollectionView.contentInset.top = topInset
|
|
messagesCollectionView.scrollIndicatorInsets.top = topInset
|
|
}
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
var keyboardOffsetFrame: CGRect {
|
|
guard let inputFrame = inputAccessoryView?.frame else { return .zero }
|
|
return CGRect(origin: inputFrame.origin, size: CGSize(width: inputFrame.width, height: inputFrame.height - iPhoneXBottomInset))
|
|
}
|
|
|
|
/// On the iPhone X the inputAccessoryView is anchored to the layoutMarginesGuide.bottom anchor
|
|
/// so the frame of the inputAccessoryView is larger than the required offset
|
|
/// for the MessagesCollectionView.
|
|
///
|
|
/// - Returns: The safeAreaInsets.bottom if its an iPhoneX, else 0
|
|
private var iPhoneXBottomInset: CGFloat {
|
|
if #available(iOS 11.0, *) {
|
|
guard UIScreen.main.nativeBounds.height == 2436 else { return 0 }
|
|
return view.safeAreaInsets.bottom
|
|
}
|
|
return 0
|
|
}
|
|
}
|