Skip to content

Commit

Permalink
Clean up keyboard handling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SD10 committed Sep 14, 2017
1 parent e2e0894 commit 957694f
Showing 1 changed file with 23 additions and 37 deletions.
60 changes: 23 additions & 37 deletions Sources/MessagesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ open class MessagesViewController: UIViewController {
open var messageInputBar = MessageInputBar()

private var messageInputBarCopy: MessageInputBar?

private var isFirstLayout: Bool = true

override open var canBecomeFirstResponder: Bool {
return true
Expand Down Expand Up @@ -69,12 +71,16 @@ open class MessagesViewController: UIViewController {

open override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
addKeyboardObservers()
removeMessageInputBarCopy()
}

override open func viewDidLayoutSubviews() {
messagesCollectionView.contentInset.top = topLayoutGuide.length
open override func viewDidLayoutSubviews() {
// Hack to prevent animation of the contentInset after viewDidAppear
if isFirstLayout {
addKeyboardObservers()
messagesCollectionView.contentInset.bottom = messageInputBar.frame.height
isFirstLayout = false
}
}

// MARK: - Initializers
Expand Down Expand Up @@ -115,14 +121,12 @@ open class MessagesViewController: UIViewController {

messagesCollectionView.translatesAutoresizingMaskIntoConstraints = false

let constraints: [NSLayoutConstraint] = [
messagesCollectionView.topAnchor.constraint(equalTo: view.topAnchor),
messagesCollectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
messagesCollectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
let top = messagesCollectionView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor)
let leading = messagesCollectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor)
let trailing = messagesCollectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
let bottom = messagesCollectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor)

messagesCollectionView.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor),
]
NSLayoutConstraint.activate(constraints)
NSLayoutConstraint.activate([top, bottom, trailing, leading])
}

// MARK: - MessageInputBar
Expand Down Expand Up @@ -257,44 +261,26 @@ extension MessagesViewController: UICollectionViewDataSource {
extension MessagesViewController {

fileprivate func addKeyboardObservers() {

NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidChangeState), name: .UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidChangeState), name: .UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidChangeState), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidChangeState), name: .UIKeyboardWillChangeFrame, object: nil)

}

fileprivate func removeKeyboardObservers() {
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillChangeFrame, object: nil)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardDidShow, object: nil)
}

func handleKeyboardDidChangeState(_ notification: Notification) {

guard let keyboardEndFrame = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? CGRect else { return }
let keyboardFrame = self.view.convert(keyboardEndFrame, from: self.view.window)

switch notification.name {
case Notification.Name.UIKeyboardDidShow:
// Only runs once
messagesCollectionView.contentInset = UIEdgeInsets(top: topLayoutGuide.length, left: 0, bottom: messageInputBar.frame.height, right: 0)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardDidShow, object: nil)
case Notification.Name.UIKeyboardDidChangeFrame, Notification.Name.UIKeyboardWillShow:
if (keyboardFrame.origin.y + keyboardFrame.size.height) > view.frame.size.height {
// Hardware keyboard is found
messagesCollectionView.contentInset = UIEdgeInsets(top: topLayoutGuide.length, left: 0, bottom: messageInputBar.frame.height, right: 0)
} else {
// Software keyboard is found
messagesCollectionView.contentInset = UIEdgeInsets(top: topLayoutGuide.length, left: 0, bottom: keyboardEndFrame.height, right: 0)
}
case Notification.Name.UIKeyboardWillHide:
messagesCollectionView.contentInset = UIEdgeInsets(top: topLayoutGuide.length, left: 0, bottom: messageInputBar.frame.height, right: 0)
default:
break

if (keyboardEndFrame.origin.y + keyboardEndFrame.size.height) > view.frame.size.height {
// Hardware keyboard is found
messagesCollectionView.contentInset.bottom = view.frame.size.height - keyboardEndFrame.origin.y
} else {
//Software keyboard is found
let bottomInset = keyboardEndFrame.height > messageInputBar.frame.height ? keyboardEndFrame.height : messageInputBar.frame.height
messagesCollectionView.contentInset.bottom = bottomInset
}

}

}

0 comments on commit 957694f

Please sign in to comment.