我正在设置一个 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/