我有一个带有一些标题视图的ViewController和一个填充所有剩余空间的UICollectionView。 (这是一个聊天应用程序)
我没有使用情节提要。我的收藏夹视图是这样声明的:
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)
然后,我可以使用这些函数来正确获取数据:
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)
}
因此,这是我所得到的示例,在调整大小之前具有良好的方面:
我要做的是在键盘出现时调整collectionview的高度。
这是我的函子中的代码,当键盘出现时会触发该代码:
self.messagesCollectionView.frame.size.height -= keyboardSize.height - (FDUtils.shared.getBottomSafeAreaHeight() ?? 0)
self.messagesCollectionView.reloadData()
self.messagesCollectionView.layoutIfNeeded()
我所有的单元格都改变了,结果完全坏了
知道我错了吗?我试图只是调整collectionView的contentInsets,但我不能使用它,因为如果使用该解决方案,滚动条的行为将不合逻辑
谢谢
答案 0 :(得分:0)
键盘抬起时添加监听器
NotificationCenter.default.addObserver(self, selector: #selector(showKeyboard), name: UIResponder.keyboardWillShowNotification, object: nil)
@objc func showKeyboard(notification: Notification) {
collectionView.scrollToItem(at: IndexPath(row: messagesList.count - 1, section: 0), at: .top, animated: false)
}
谢谢
答案 1 :(得分:0)
而不是更改collectionView的高度,而是更改collectionView的contentInset
以下代码将为您提供帮助。
@IBOutlet weak var collectionViewBottomConstraint: NSLayoutConstraint!
func addKeyboardNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillhide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}
@objc func keyboardWillAppear(notification: Notification) {
if var keyBoardHeight = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject?)?.cgRectValue?.size.height {
if UIScreen.main.bounds.height >= 812.0 {
keyBoardHeight -= 34.0
}
UIView.animate(withDuration: 1.0) {
//self.collectionViewBottomConstraint.constant = keyBoardHeight
collectionView.contentInset.bottom = keyBoardHeight
self.view.layoutIfNeeded()
}
}
}
@objc func keyboardWillhide(notification: Notification) {
collectionView.contentInset = UIEdgeInsets.zero
UIView.animate(withDuration: 1.0) {
self.view.layoutIfNeeded()
}
}
答案 2 :(得分:0)
在键盘启动时尝试下一行,因为重新加载不会更改您的单元格框架,因此您需要调用invalidateLayout()函数。
ngModel
答案 3 :(得分:0)
刚刚找到了答案,这要感谢他们的帮助
问题出在我的cellForRowAt函数中。
我的func cell.createMessage并没有调整白色单元格的大小以确保消息良好,我不确定为什么会这样,因为我的sizeForItemAt函数看起来不错,但是我只是通过直接重新计算单元格高度来设法解决了这个问题createMessage函数