ios - UICollectionView 布局在滚动时表现异常
问题描述
我正在设置一个集合视图,以便在 Instagram 上显示内容。Instagram 的网格
为了设置布局,我使用了UIcollectionviewDelegateFlowLayout的一些委托方法:
extension ProfileViewController : UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat{
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat{
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (view.frame.width)/3, height: (view.frame.width)/3)
}
}
当我打开项目时,collectionView 运行良好。当您快速向下滚动 collectionView 以使所有单元格瞬间消失时,就会出现问题。在那一刻,我之前设置的单元格布局不成立,图像变得超级缩放并且彼此重叠,如下所示:混乱的 collectionView 我不知道如何解决这个问题。同样在编译器中我得到这个错误:
the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
Please check the values returned by the delegate.
The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fbbe972d1f0>, and it is attached to <UICollectionView: 0x7fbbeb056400; frame = (0 0; 414 896); clipsToBounds = YES; autoresize = RM+BM;
gestureRecognizers = <NSArray: 0x60000203c4e0>; layer = <CALayer: 0x600002ef19c0>; contentOffset: {0, 424}; contentSize: {414, 16048}; adjustedContentInset: {88, 0, 83, 0}; layout: <UICollectionViewFlowLayout: 0x7fbbe972d1f0>; dataSource: <InstagramClone.ProfileViewController: 0x7fbbe9708890>>.
解决方案
我认为您的问题可能与本文中的问题相同。
尝试通过将 设置UICollectionView's estimatedSize
为无来解决您的问题。
这是因为在 Xcode 11 中,CollectionView 中的单元格现在可以使用画布中的自动布局约束视图自行调整大小。
推荐阅读
- kubernetes - “oc进程”将特殊字符“&”和“>”转换为unicode
- python - 无法从数据框中创建简单的 dict
- installation - 对使用 Homebrew 安装的脚本设置可执行权限
- java - 如何修复 Google 移动广告集成错误
- excel - 循环搜索同一行特定范围列的文本,一旦找到,在不同行但同一列中返回值
- php - 首次安装 Grav 时如何解决 PHPModules 错误?
- nginx - Nginx:Access-Control-Allow-Origin 不适用于特定位置
- python - 如何在 python 2.7 中刷新 StringIO()?
- mysql - 查找并返回前员工但不是当前员工
- css - 将 NativeScript 主题应用于从 NativeScript 模板下载的项目