How to add loading Indicator in Footer of CollectionView in iOS

后端 未结 5 1917
迷失自我
迷失自我 2021-01-05 09:49

How can I add loading Indicator in the Footer of collection view when data is loaded for next page..?

I want to loading indicator in Footer of Collection vi

5条回答
  •  不知归路
    2021-01-05 09:51

    The simplest solution would be adding the activity indicator to a view and add that view to UICollectionReusableView.
    
    In collection view : 
    
     private let footerView = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.white)
    
    
      override func viewDidLoad() {
            super.viewDidLoad()
    
            collectionView.register(CollectionViewFooterView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "Footer")
            (collectionView.collectionViewLayout as? UICollectionViewFlowLayout)?.footerReferenceSize = CGSize(width: collectionView.bounds.width, height: 50)
    
        }
    
       override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
            if kind == UICollectionView.elementKindSectionFooter {
                let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Footer", for: indexPath)
                footer.addSubview(footerView)
                footerView.frame = CGRect(x: 0, y: 0, width: collectionView.bounds.width, height: 50)
                return footer
            }
            return UICollectionReusableView()
        }
    
    customized class: add your customizations here if you need any
    
    public class CollectionViewFooterView: UICollectionReusableView {
        override init(frame: CGRect) {
            super.init(frame: frame)
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
    just use: footerView.startAnimating() & footerView.stopAnimating() for start or stop the animation 
    

    ***** Hope you like it .******

提交回复
热议问题