Cell spacing in UICollectionView

后端 未结 26 3242
旧巷少年郎
旧巷少年郎 2020-11-22 15:53

How do I set cell spacing in a section of UICollectionView? I know there is a property minimumInteritemSpacing I have set it to 5.0 still the spaci

相关标签:
26条回答
  • 2020-11-22 16:42

    Swift 3 Version

    Simply create a UICollectionViewFlowLayout subclass and paste this method.

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
            guard let answer = super.layoutAttributesForElements(in: rect) else { return nil }
    
            for i in 1..<answer.count {
                let currentAttributes = answer[i]
                let previousAttributes = answer[i - 1]
    
                let maximumSpacing: CGFloat = 8
                let origin = previousAttributes.frame.maxX
    
                if (origin + maximumSpacing + currentAttributes.frame.size.width < self.collectionViewContentSize.width && currentAttributes.frame.origin.x > previousAttributes.frame.origin.x) {
                    var frame = currentAttributes.frame
                    frame.origin.x = origin + maximumSpacing
                    currentAttributes.frame = frame
                }
            }
    
            return answer
    }
    
    0 讨论(0)
  • 2020-11-22 16:42

    Try playing around with this method:

    - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                        layout:(UICollectionViewLayout*)collectionViewLayout
        insetForSectionAtIndex:(NSInteger)section {
    
        UIEdgeInsets insets = UIEdgeInsetsMake(?, ?, ?, ?);
    
        return insets;
    }
    
    0 讨论(0)
提交回复
热议问题