'Pull to refresh in UICollectionView in ViewController

I used the following code at a UICollectionViewController

override func viewDidLoad() {            

    self.collectionView!.alwaysBounceVertical = true
    let refresher = UIRefreshControl()
    refresher.addTarget(self, action: "refreshStream", forControlEvents: .ValueChanged)

    refreshControl = refresher
    collectionView!.addSubview(refreshControl!)
}

func refreshStream() {

    print("refresh")
    self.collectionView?.reloadData()

    refreshControl?.endRefreshing()

}

Now I need it to work with a UICollectionView inside a UIViewController and I googled for an hour now but can't get it working. I appreciate any help.



Solution 1:[1]

From the storyboard, you need to link the collection view to the appropriate controller file, using Ctrl + Drag from the collection view object. It needs to be linked via an @IBOutlet.

Also, you should Ctrl + Drag from the collection view to the view controller object on the storyboard and select Data Source and Delegate so that the collection view is correctly linked.

Let me know if this helps or if I have misunderstood your situation.

Solution 2:[2]

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()
 }

Solution 3:[3]

Swift 5 solution

As @fishspy already mentioned, that's the way to put your collection view inside a view controller, but I'm gonna share also how to connect your refresh control to the collection view in a cleaner way.

Since iOS 10 there's a dedicated property for the refresh control. Apart of that, I'd also recommend to directly initialise your refresh control as a property, declaring it private and doing the following things:

@IBOutlet private weak var collectionView: UICollectionView!

private let refreshControl = UIRefreshControl()

override func viewDidLoad() {

    super.viewDidLoad()

    refreshControl.addTarget(self, action: #selector(didPullToRefresh(_:)), for: .valueChanged)
    collectionView.alwaysBounceVertical = true
    collectionView.refreshControl = refreshControl // iOS 10+
}

@objc
private func didPullToRefresh(_ sender: Any) {
    // Do you your api calls in here, and then asynchronously remember to stop the
    // refreshing when you've got a result (either positive or negative)
    refreshControl.endRefreshing()
}

Solution 4:[4]

var refreshControl:UIRefreshControl!

override func viewDidLoad() {
    super.viewDidLoad()
      self.refreshControl = UIRefreshControl()
      self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
     self.refreshControl.addTarget(self, action: #selector(PricingPlansCollectionViewController.reload), for: .valueChanged)
      collectionView!.addSubview(refreshControl)
}

 func refresh(sender:AnyObject)
  {
    //DO 
  }

Solution 5:[5]

For me, i had:

func setupCollectionView(){
      self.refreshControl = UIRefreshControl()
      self.refreshControl.attributedTitle = NSAttributedString(string: "Refreshing content...")
      self.refreshControl.addTarget(self, action: #selector(self.reload), for: .valueChanged)
      collectionView!.addSubview(refreshControl)
}

It still wasn't working (I believe i had constraints preventing it somehow), so i added:

collectionView.alwaysBounceVertical = true

then all was good. Just in case someone else is facing the same issue.

For a better understanding, the full implementation

@IBOutlet private weak var collectionView: UICollectionView!
let refreshControl = UIRefreshControl()

override func viewDidLoad() {
 super.viewDidLoad()
 setupCollectionView()
}

func setupCollectionView(){
      collectionView.delegate = self
      collectionView.dataSource = self
      collectionView.alwaysBounceVertical = true
      self.refreshControl = UIRefreshControl()
      self.refreshControl.attributedTitle = NSAttributedString(string: "Refreshing content...")
      self.refreshControl.addTarget(self, action: #selector(self.refresh), for: .valueChanged)
      collectionView!.addSubview(refreshControl)
}

 @objc func refresh(_ sender:AnyObject) {
    //do refreshing stuff
}

Solution 6:[6]

private let refreshControl = UIRefreshControl()

refreshControl.addTarget(self, action: #selector(youFunction), for: .valueChanged)
   
refreshControl.attributedTitle = NSAttributedString(string: "Fetching Data ...", attributes: nil)

DispatchQueue.main.async {
       self.refreshControl.endRefreshing()
       yourCollectionViewOrTableView.reloadData()
 }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fishspy
Solution 2 Ghulam Rasool
Solution 3 Alessandro Francucci
Solution 4 Romulo BM
Solution 5 tendai
Solution 6