美文网首页
Swift-设置UICollectionView背景图

Swift-设置UICollectionView背景图

作者: PerhapYs | 来源:发表于2022-03-17 17:20 被阅读0次
    
    import UIKit
    
    protocol PYCollectionBackgroundViewDelegate : AnyObject{
        // 返回的背景图视图
        func backgroundViewForCollectionView() -> UICollectionReusableView.Type
    }
    
    class PYCollectionBackgroundViewFlowLayout: UICollectionViewFlowLayout {
    
        private let identifier : String = "identifierForBgFlowLayout"
        
        var backgroundViewHeight : CGFloat = UIScreen.main.bounds.size.height
        
        var backgroundViewWidth : CGFloat = UIScreen.main.bounds.size.width
        
        weak var pyDelegate : PYCollectionBackgroundViewDelegate?
        
        override func layoutAttributesForDecorationView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
            
            guard let delegate = self.pyDelegate else {
                
                return nil
            }
            
            let className: UICollectionReusableView.Type = delegate.backgroundViewForCollectionView()
            
            self.register(className, forDecorationViewOfKind: identifier)
            
            let atts = UICollectionViewLayoutAttributes.init(forDecorationViewOfKind: identifier, with: indexPath)
            
            atts.frame = CGRect.init(x: 0, y: 0, width: backgroundViewWidth, height: backgroundViewHeight)
            
            atts.zIndex = -1
            
            return atts
        }
            
        override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
            
            let supAtts = super.layoutAttributesForElements(in: rect)
            var atts : [UICollectionViewLayoutAttributes] = []
            
            if let supAtts : [UICollectionViewLayoutAttributes] = supAtts {
                atts = NSMutableArray.init(array: supAtts) as! [UICollectionViewLayoutAttributes]
            }
            let a = NSIndexPath.init(row: 0, section: 0)
            let att = self.layoutAttributesForDecorationView(ofKind: identifier, at:a as IndexPath)
            guard let unwapAtt = att else {
                return atts
            }
            atts.append(unwapAtt)
            return atts
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Swift-设置UICollectionView背景图

          本文链接:https://www.haomeiwen.com/subject/ctavdrtx.html