Skip to content

Commit d2fee18

Browse files
committed
Fixed MessagesCollectionView's bottom inset when hardware keyboard is connected
1 parent 0ceeb49 commit d2fee18

3 files changed

Lines changed: 27 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ The changelog for `MessageKit`. Also see the [releases](https://github.com/Messa
88

99
### Fixed
1010

11+
- Fixed `MessagesCollectionView`'s bottom inset when hardware keyboard is connected. Do not adjust `MessagesCollection`'s inset when being popped.
12+
[#707](https://github.com/MessageKit/MessageKit/pull/707) by [@zhongwuzw](https://github.com/zhongwuzw).
13+
1114
- Fixed `LabelAlignment` initializer which has default internal access control.
1215
[#705](https://github.com/MessageKit/MessageKit/pull/705) by [@zhongwuzw](https://github.com/zhongwuzw).
1316

Sources/Controllers/MessagesViewController+Keyboard.swift

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,19 @@ extension MessagesViewController {
5353
@objc
5454
private func handleKeyboardDidChangeState(_ notification: Notification) {
5555
guard let keyboardEndFrame = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? CGRect else { return }
56-
57-
if (keyboardEndFrame.origin.y + keyboardEndFrame.size.height) > UIScreen.main.bounds.height {
58-
// Hardware keyboard is found
59-
messageCollectionViewBottomInset = view.frame.size.height - keyboardEndFrame.origin.y - iPhoneXBottomInset
60-
} else {
61-
//Software keyboard is found
62-
let afterBottomInset = keyboardEndFrame.height > keyboardOffsetFrame.height ? (keyboardEndFrame.height - iPhoneXBottomInset) : keyboardOffsetFrame.height
63-
let differenceOfBottomInset = afterBottomInset - messageCollectionViewBottomInset
64-
65-
if maintainPositionOnKeyboardFrameChanged && differenceOfBottomInset != 0 {
66-
let contentOffset = CGPoint(x: messagesCollectionView.contentOffset.x, y: messagesCollectionView.contentOffset.y + differenceOfBottomInset)
67-
messagesCollectionView.setContentOffset(contentOffset, animated: false)
68-
}
69-
70-
messageCollectionViewBottomInset = afterBottomInset
56+
57+
guard !isMessagesControllerBeingDismissed else { return }
58+
59+
let newBottomInset = view.frame.height - keyboardEndFrame.minY - iPhoneXBottomInset
60+
61+
let differenceOfBottomInset = newBottomInset - messageCollectionViewBottomInset
62+
63+
if maintainPositionOnKeyboardFrameChanged && differenceOfBottomInset != 0 {
64+
let contentOffset = CGPoint(x: messagesCollectionView.contentOffset.x, y: messagesCollectionView.contentOffset.y + differenceOfBottomInset)
65+
messagesCollectionView.setContentOffset(contentOffset, animated: false)
7166
}
67+
68+
messageCollectionViewBottomInset = newBottomInset
7269
}
7370

7471
@objc

Sources/Controllers/MessagesViewController.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
6060
}
6161

6262
private var isFirstLayout: Bool = true
63+
64+
internal var isMessagesControllerBeingDismissed: Bool = false
6365

6466
internal var selectedIndexPathForMenu: IndexPath?
6567

@@ -82,6 +84,16 @@ UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
8284
addObservers()
8385
}
8486

87+
open override func viewDidAppear(_ animated: Bool) {
88+
super.viewDidAppear(animated)
89+
isMessagesControllerBeingDismissed = false
90+
}
91+
92+
open override func viewWillDisappear(_ animated: Bool) {
93+
super.viewWillDisappear(animated)
94+
isMessagesControllerBeingDismissed = true
95+
}
96+
8597
open override func viewDidLayoutSubviews() {
8698
// Hack to prevent animation of the contentInset after viewDidAppear
8799
if isFirstLayout {

0 commit comments

Comments
 (0)