Pull to refresh in UICollectionView in ViewController

New swift code changed in calling action method you could do rewrite like this

@IBOutlet weak var collectionView: UICollectionView!

var refresher:UIRefreshControl!

override func viewDidLoad() {

   super.viewDidLoad()

    self.refresher = UIRefreshControl()
    self.collectionView!.alwaysBounceVertical = true
    self.refresher.tintColor = UIColor.red
    self.refresher.addTarget(self, action: #selector(loadData), for: .valueChanged)
    self.collectionView!.addSubview(refresher)
}

@objc func loadData() {
   self.collectionView!.refreshControl.beginRefreshing()
   //code to execute during refresher
       .
       .
       .
   stopRefresher()         //Call this to stop refresher
 }

func stopRefresher() {
   self.collectionView!.refreshControl.endRefreshing()
 }

Leave a Comment