I have a ViewController with some header view in it and a UICollectionView who fill all the remaining space. (it's a chat app)
I'm not using storyboard. My collection view is declared this way :
self.messagesCollectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height - barHeight), collectionViewLayout: UICollectionViewFlowLayout())
messagesCollectionView.register(MessageCollectionViewCell.self, forCellWithReuseIdentifier: self.cellReuseIdentifier)
messagesCollectionView.delegate = self
messagesCollectionView.dataSource = self
self.view.addSubview(messagesCollectionView)
Then I have theses func to get my data correctly :
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
self.conversation?.messagesList.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.cellReuseIdentifier, for: indexPath) as! MessageCollectionViewCell
if let message = self.conversation?.messagesList[indexPath.row] {
cell.createMessage(message: message)
}
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if let message = self.conversation?.messagesList[indexPath.row] {
var heightNeeded = message.text.height(withConstrainedWidth: FDDimensions.shared.k_MessageMaxWidth, font: FDFonts.Montserrat_Regular_14 ?? UIFont.systemFont(ofSize: 14))
heightNeeded += 22
heightNeeded += 25
return CGSize(width: self.view.frame.width, height: heightNeeded)
} else {
return CGSize(width: 0, height: 0)
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 10
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets.init(top: 10, left: 0, bottom: 10, right: 0)
}
So here is an example of what I get, with good aspect before resizing it :
The thing I want to do is resize my collectionview height when the keyboard appears.
Here is the code in my func who is triggered when the keyboard show up :
self.messagesCollectionView.frame.size.height -= keyboardSize.height - (FDUtils.shared.getBottomSafeAreaHeight() ?? 0)
self.messagesCollectionView.reloadData()
self.messagesCollectionView.layoutIfNeeded()
All my cells are changed and the result is completely broken
Any idea where I'm wrong ? I've tried to just ajust the contentInsets of the collectionView but I can't use that because the scrollbar behavior won't be logic if I use that solution
Thanks
Just found the answer, thanks to them who helped
The problem was in my cellForRowAt func.
My func cell.createMessage wasn't resizing my white cell for message good, I'm not sure why because my sizeForItemAt func seems to works great but I managed to fix the problem just by re-calculing my cell height directly in the createMessage func