自定义collectionviewflowLayout

class CustomFlowLayout: UICollectionViewFlowLayout {
    var attributeArr: [UICollectionViewLayoutAttributes] = []
    var totalHeight: CGFloat = 0
    
    //insetArr(用来没放每组中的上线左右的距离)
    var insetDict: [Int: UIEdgeInsets] = [:]
    var minLineDict: [Int: CGFloat] = [:]
    var minInterDict: [Int: CGFloat] = [:]
    override func prepare() {
        super.prepare()
        guard let collectionView = self.collectionView else { return }
        let sections =  self.collectionView?.numberOfSections ?? 0
        ///左边距离的位置
        var leftWidth: CGFloat = 0
        var superWidth: CGFloat = collectionView.bounds.width
        var leaveWidth: CGFloat = superWidth
        for indexPathSection in 0..<sections {
            let numbersOfSection = self.collectionView?.numberOfItems(inSection: indexPathSection) ?? 0
            let inset = insetDict[indexPathSection] ?? UIEdgeInsets.zero
            let minInter = minInterDict[indexPathSection] ?? 0
            let minLine = minLineDict[indexPathSection] ?? 0


            //设置组头的布局
            if let headerAttribute = self.layoutAttributesForSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, at: IndexPath.init(item: 0,section: indexPathSection)) {
                headerAttribute.frame = CGRect.init(x: 0, y: totalHeight, width: headerAttribute.frame.width, height: headerAttribute.frame.height)
                totalHeight += inset.top + headerAttribute.frame.height
                attributeArr.append(headerAttribute)
            }
            
            for indexPathItem in 0..<numbersOfSection {
                let indexPath = IndexPath.init(item: indexPathItem, section: indexPathSection)
                let itemSize = self.layoutAttributesForItem(at: indexPath)?.size ?? CGSize.zero
                if let cellAttribute = self.layoutAttributesForItem(at: indexPath) {
                    if indexPath.item == 0 {
                        leftWidth = inset.left
                        leaveWidth = superWidth - leftWidth - inset.right
                    }
                    if leaveWidth < itemSize.width {
                        leftWidth = inset.left
                        totalHeight += itemSize.height + minLine
                        leaveWidth = superWidth - leftWidth - inset.right
                    }
                    cellAttribute.frame = CGRect.init(x: leftWidth, y: totalHeight, width: itemSize.width, height: itemSize.height)
                    leftWidth += minInter + itemSize.width
                    leaveWidth = superWidth - leftWidth - inset.right
                    attributeArr.append(cellAttribute)
                    if indexPathItem == (numbersOfSection - 1) {
                        totalHeight += itemSize.height
                    }

                }
                
                
                
            }
            //设置组尾布局
            if let footerAttribure = self.layoutAttributesForSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, at: IndexPath.init(item: 0, section: indexPathSection)) {
                footerAttribure.frame = CGRect.init(x: 0, y: totalHeight + inset.bottom, width: footerAttribure.frame.width, height: footerAttribure.frame.height)
                totalHeight += footerAttribure.frame.height + inset.bottom
                attributeArr.append(footerAttribure)
            }
            

        }

        
    }
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        return self.attributeArr
    }
    
    
//    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
//        return true
//    }
    override var collectionViewContentSize: CGSize{
        get {
            return CGSize.init(width: self.collectionView?.bounds.width ?? 0, height: self.totalHeight)
        }
    }
    
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容