@@ -44,6 +44,7 @@ public class MessagesView: UIView {
4444 @IBOutlet weak var messageInputToolbarBottomConstraint : NSLayoutConstraint !
4545
4646 private var toolBarBottomConstraintWithoutKeyboard : CGFloat = 0
47+ private var toolBarFrameWithoutKeyboard : CGRect = . zero
4748
4849 //MARK:- Public properties
4950
@@ -97,6 +98,8 @@ public class MessagesView: UIView {
9798 var bubbleImageLeft : BubbleImage = BubbleImage ( cornerRadius: 8 )
9899 var bubbleImageRight : BubbleImage = BubbleImage ( cornerRadius: 8 ) . flipped
99100
101+ private var isKeyboardShown = false
102+
100103 public func setBubbleImagesWith( left: BubbleImage , right: BubbleImage ? = nil ) {
101104
102105 bubbleImageLeft = left
@@ -207,49 +210,62 @@ public class MessagesView: UIView {
207210
208211 NotificationCenter . default. addObserver ( self , selector: #selector( keyboardWillShow) , name: . UIKeyboardWillShow, object: nil )
209212 NotificationCenter . default. addObserver ( self , selector: #selector( keyboardWillHide) , name: . UIKeyboardWillHide, object: nil )
213+ NotificationCenter . default. addObserver ( self , selector: #selector( keyboardWillChangeFrame) , name: . UIKeyboardWillChangeFrame, object: nil )
210214 }
211215
212216 @objc private func keyboardWillShow( notification: Notification ) {
213217
214- guard settings. shouldAdjustToKeyboard,
215- let userInfo = notification. userInfo,
216- let keyboardFrame = ( userInfo [ UIKeyboardFrameEndUserInfoKey] as? NSValue ) ? . cgRectValue,
217- let animationDuration = ( userInfo [ UIKeyboardAnimationDurationUserInfoKey] as? NSNumber ) ? . doubleValue else {
218+ guard !isKeyboardShown else {
218219 return
219220 }
220221
221- toolBarBottomConstraintWithoutKeyboard = messageInputToolbarBottomConstraint . constant
222+ isKeyboardShown = true
222223
223- let toolbarFrameInWindow = convert ( messagesInputToolbar. frame, to: nil )
224+ toolBarBottomConstraintWithoutKeyboard = messageInputToolbarBottomConstraint. constant
225+ toolBarFrameWithoutKeyboard = convert ( messagesInputToolbar. frame, to: nil )
224226
225- let keyboardOverlap = toolbarFrameInWindow. origin. y - keyboardFrame. origin. y
227+ respondToKeyboardFrameChange ( notification: notification)
228+ }
229+
230+ @objc private func keyboardWillChangeFrame( notification: Notification ) {
226231
227- guard keyboardOverlap > 0 else {
232+ guard isKeyboardShown else {
228233 return
229234 }
230235
231- let verticalAdjusttment = keyboardOverlap + toolbarFrameInWindow. size. height
232-
233- messageInputToolbarBottomConstraint. constant = toolBarBottomConstraintWithoutKeyboard + verticalAdjusttment
234-
235- UIView . animate ( withDuration: animationDuration) {
236- let contentOffset = self . messagesCollectionView. contentOffset
237-
238- self . messagesCollectionView. contentOffset = CGPoint ( x: contentOffset. x, y: contentOffset. y + verticalAdjusttment)
239- self . layoutIfNeeded ( )
240- }
236+ respondToKeyboardFrameChange ( notification: notification)
241237 }
242238
243239 @objc private func keyboardWillHide( notification: Notification ) {
244240
241+ isKeyboardShown = false
242+ }
243+
244+ private func respondToKeyboardFrameChange( notification: Notification ) {
245+
245246 guard settings. shouldAdjustToKeyboard,
246- let animationDuration = ( notification. userInfo ? [ UIKeyboardAnimationDurationUserInfoKey] as? NSNumber ) ? . doubleValue else {
247+ let userInfo = notification. userInfo,
248+ let keyboardFrame = ( userInfo [ UIKeyboardFrameEndUserInfoKey] as? NSValue ) ? . cgRectValue,
249+ let animationDuration = ( userInfo [ UIKeyboardAnimationDurationUserInfoKey] as? NSNumber ) ? . doubleValue else {
250+ return
251+ }
252+
253+ let keyboardOverlap = toolBarFrameWithoutKeyboard. origin. y - keyboardFrame. origin. y
254+
255+ let verticalAdjustment = keyboardOverlap > 0 ? keyboardOverlap + toolBarFrameWithoutKeyboard. size. height : 0
256+
257+ let newBottomConstraint = toolBarBottomConstraintWithoutKeyboard + verticalAdjustment
258+
259+ guard newBottomConstraint != messageInputToolbarBottomConstraint. constant else {
247260 return
248261 }
249262
250- messageInputToolbarBottomConstraint. constant = toolBarBottomConstraintWithoutKeyboard
263+ messageInputToolbarBottomConstraint. constant = newBottomConstraint
251264
252265 UIView . animate ( withDuration: animationDuration) {
266+ let contentOffset = self . messagesCollectionView. contentOffset
267+
268+ self . messagesCollectionView. contentOffset = CGPoint ( x: contentOffset. x, y: contentOffset. y + verticalAdjustment)
253269 self . layoutIfNeeded ( )
254270 }
255271 }
0 commit comments