Skip to content

Commit 46bebec

Browse files
authored
Merge pull request MessageKit#707 from zhongwuzw/fix-bottom-inset
Fixed MessagesCollectionView's bottom inset when hardware keyboard is…
2 parents 818a881 + 95f3921 commit 46bebec

6 files changed

Lines changed: 46 additions & 24 deletions

File tree

CHANGELOG.md

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

77
## Upcoming release
88

9+
### Fixed
10+
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+
14+
- Fixed `LabelAlignment` initializer which has default internal access control.
15+
[#705](https://github.com/MessageKit/MessageKit/pull/705) by [@zhongwuzw](https://github.com/zhongwuzw).
16+
917
### Added
1018

1119
- Added convenience method to set properties for all `MessageSizeCalculator`s.

Documentation/FAQs.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ func configureAvatarView(_ avatarView: AvatarView, for message: MessageType, at
3131
}
3232
```
3333

34-
If you return `CGSize.zero` from the `MessagesLayoutDelegate` method
35-
`avatarSize(for:MessageType,at:IndexPath,in:MessagesCollectionView)`, the `AvatarView`
36-
will not be visible for that cell.
34+
If you also like to remove the space the `AvatarView` occupies you have to change the properties
35+
`outgoingAvatarSize` or `incomingAvatarSize` of the `CellSizeCalculator` object for the respective message to `CGSize.zero`.
3736

3837
```Swift
39-
func avatarSize(for: MessageType, at: IndexPath, in: MessagesCollectionView) -> CGSize {
40-
return .zero
38+
if let layout = messagesCollectionView.collectionViewLayout as? MessagesCollectionViewFlowLayout {
39+
layout.textMessageSizeCalculator.outgoingAvatarSize = .zero
40+
layout.textMessageSizeCalculator.incomingAvatarSize = .zero
4141
}
4242
```
4343

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 {

Sources/Models/LabelAlignment.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public struct LabelAlignment {
2828

2929
public var textAlignment: NSTextAlignment
3030
public var textInsets: UIEdgeInsets
31+
32+
public init(textAlignment: NSTextAlignment, textInsets: UIEdgeInsets) {
33+
self.textAlignment = textAlignment
34+
self.textInsets = textInsets
35+
}
3136

3237
}
3338

VISION.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
- Cultivate an inclusive open source community through respectful discussion.
99

1010
### Scope
11-
MessageKit will only concern itself with the **MessagesViewController**.
11+
#### Things we will not support:
12+
- Views that live outside of the MessagesViewController such as photo pickers and media players.
13+
- Anything that favors specific networking / caching strategies or specific libraries.
1214

13-
We will **not** support other views such as: **Photo Pickers** and **Video/Music Players**.
14-
15-
Instead, MessageKit will provide you with hooks to easily handle your different message types and present a view of your choice. We believe this is more flexible, maintainable, and reasonable.
15+
Instead, MessageKit will provide you with hooks to easily handle these situations as you wish. We believe this is more flexible, maintainable, and reasonable.
1616

1717
### Technical Considerations
1818
- **iOS version**:

0 commit comments

Comments
 (0)