HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 集合中的可重用单元的子视图在滚动后具有错误的大小大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个用于聊天页面的collectionView.我创建了一个名为TextChatCollectionViewCell的UICollectionViewCell子类.
我有一个类填充我的集合视图并为每个项指定一个CGSize(遵循UICollectionViewDelegateFlowLayout和UICollectionViewDatasource协议).

我滚动时单元格框架大小正确但是子视图的框架大小错误,可能是因为dequeueReusableCell返回另一个单元格的实例并且子视图没有重新加载,我试调用layoutIfNeeded()来强制再次绘制子视图的布局但是它没有效果.

我的单元格的xib文件有约束:

ios – 集合中的可重用单元的子视图在滚动后具有错误的大小

我填写并返回单元格的代码

public func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = self.manager.collectionView.dequeueReusableCell(withReusEIDentifier: "TextChatCollectionViewCell",for: indexPath) as? TextChatCollectionViewCell else {
        return UICollectionViewCell()
    }

    self.fillCellUsingmessageUUID(cell,self.messagesUUIDs[indexPath.row]) // Hide labelDate (set height to 0 for the moment),fill UITextView etc...


    print("=================\(self.messagesUUIDs[indexPath.row])===================")
    print("Cell text : " + cell.textmessage.text)
    print("Cell frame size : " + cell.frame.size.debugDescription)
    print("UITextView frame size : " + cell.textmessage.frame.size.debugDescription)
    print("UITextView parent size : " + cell.textBACkgroundView.frame.size.debugDescription)
    print("Cell content view frame size : " + cell.globalView.frame.size.debugDescription)
    print("====================================")
    return cell
}

我的调试输出

=================B00C74D6-C3F1-4039-948B-0BAC59DC0D83===================

Cell text : "Ggg"
Cell frame size : (320.0,80.0) // Expected size
UITextView frame size : (240.0,59.5) // Wrong height
UITextView parent size : (240.0,59.5) // Wrong height
Cell content view frame size : (320.0,59.5) // Wrong height
====================================
=================2704FFF5-17D1-4E0E-9399-DD7EB4C60D36===================
Cell text : "Zyehdhzhdhdhzhejajeksvshdjajdjhhhjhhjjhhhhthhytgjuhjjyghjuyghhuygghuutghjutghkiygvcwqazdxcfeerggvbhtyjjnkkuilloomppolpôkkjjîîîukhgkurghhhhhgoohgosohsohdohsohsohshowlhdlhslhslhsglslydlgsotwyod [...]"
Cell frame size : (320.0,411.5) // Expected size
UITextView frame size : (240.0,59.5) // Wrong height

预期产量:

=================B00C74D6-C3F1-4039-948B-0BAC59DC0D83===================

Cell text : "Ggg"
Cell frame size : (320.0,80.0)
UITextView frame size : (240.0,80.0)
UITextView parent size : (240.0,80.0)
Cell content view frame size : (320.0,80.0)
====================================
=================2704FFF5-17D1-4E0E-9399-DD7EB4C60D36===================
Cell text : "Zyehdhzhdhdhzhejajeksvshdjajdjhhhjhhjjhhhhthhytgjuhjjyghjuyghhuygghuutghjutghkiygvcwqazdxcfeerggvbhtyjjnkkuilloomppolpôkkjjîîîukhgkurghhhhhgoohgosohsohdohsohsohshowlhdlhslhslhsglslydlgsotwyod [...]"
Cell frame size : (320.0,411.5) 
UITextView frame size : (240.0,411.5) 
UITextView parent size : (240.0,411.5)
Cell content view frame size : (320.0,411.5)

解决方法

在为单元格分配新值之前,可能没有更新约束.

这是一个代码,我希望能解决它.
layoutIfNeeded()

只需在为其分配新值之前在Cell上调用它,例如

cell.layoutIfNeeded()

为我解决了很多问题.希望它能帮到你.

大佬总结

以上是大佬教程为你收集整理的ios – 集合中的可重用单元的子视图在滚动后具有错误的大小全部内容,希望文章能够帮你解决ios – 集合中的可重用单元的子视图在滚动后具有错误的大小所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。