I am working with chat screens where I have a InputAccessoryView
where user type message and UICollectionView
which contains chats.
I want to display latest chat message when user send it in UICollectionView.
Problem is when user types message InputAccessoryView comes up to a keyboard and UICollectionView goes behind to Keyboard so latest messages are not visible.
I want to move UICollectionView up to keyboard height when keyboard appears.
I am using following code.
This will register observer for keyboard hide and show events.
func handleKeyBoard(){
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
var contentInset = self.collectionView?.contentInset
contentInset?.bottom = keyboardSize.height
self.collectionView?.contentInset = contentInset!
if self.messages.count > 0{
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.view.layoutIfNeeded()
}, completion: { (completed:Bool) in
let indexPath = IndexPath(item: self.messages.count - 1, section: 0)
self.collectionView?.scrollToItem(at: indexPath, at: .bottom, animated: true)
})
}
}
}
In above function when keyboard appears I am getting it's height which always comes as 50 I don't know why may be because of InputAccessoryView
.
After getting keyboard height I am adding 50 more as a height for InputAccessoryView
and then changing the contentInset
of a UICollectionViewController
and in the completion of animation I am scrolling to a last message which will scroll UICollectionViewController to it's last message.
But it is not happening.
This is my default contentInset
for UICollectionView.
UIEdgeInsets(top: 8, left: 0, bottom: 52, right: 0)
after
var contentInset = self.collectionView?.contentInset
contentInset?.bottom = keyboardSize.height
self.collectionView?.contentInset = contentInset!
contentInset becomes this.
UIEdgeInsets(top: 8, left: 0, bottom: 52, right: 0)
Following will set defaul Inset for collectionview once keyboard hides
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.collectionView?.contentInset = UIEdgeInsets(top: 8, left: 0, bottom: 52, right: 0)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.view.layoutIfNeeded()
}, completion: { (completed:Bool) in
})
}
}
can anyone help me to find right approach to do this. That will be very helpful. Almost all chatting app have this feature where when they type then can see a new message in there chat view.
Thank You.