ios - UICollectionView 布局在滚动时表现异常

标签 ios swift uicollectionview uicollectionviewdelegate uicollectionviewdelegateflowlayout

我正在设置一个 Collection View ,以便在 Instagram 上显示内容。 instagram's grid

为了设置布局,我使用了 UIcollectionviewDelegateFlowLayout 的一些委托(delegate)方法:

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 时会出现问题,从而使所有单元格瞬间消失。 在那一刻,我之前设置的单元格布局不成立,图像变得 super 缩放并且一个在彼此之上,如下所示:messed Up 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>>.

最佳答案

我认为您的问题可能与此 post 中的问题相同.

尝试通过将 UICollectionView's estimatedSize 设置为 none 来解决您的问题。

这是因为在 Xcode 11 中,CollectionView 中的单元格现在可以使用 Canvas 中的自动布局约束 View 自行调整大小。

关于ios - UICollectionView 布局在滚动时表现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59013732/

相关文章:

ios - 使用适用于 iOS < 11 的 AudioKit 进行离线音频渲染

swift - 检查文件是否存在于 Web 服务器上的最简单解决方案。 ( swift )

ios6 - UICollectionView:UICollectionViewCell 对象在 sizeForItemAtIndexPath 中始终为空

ios - 如何在 iOS Swift 3 中以编程方式创建 UICollectionView 类而不使用 Storyboard?

ios - 如何在 View Controller 中多次加载相同的 View

ios - 更改 if 语句的 Swift 约束

ios - 从 iOS Swift 中的 xml 文本文件中提取 Json

ios - 使用 SwiftUI 时无法正确调整 SKScene 的大小

swift - 在自动调整大小的 Collection View 中插入行 - 故障

ios - 重新加载 UIPickerView