Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pull-to-refresh in UICollectionViewController

I want to implement pull-down-to-refresh in a UICollectionViewController under iOS 6. This was easy to achieve with a UITableViewController, like so:

UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; [refreshControl addTarget:self action:@selector(startRefresh:)     forControlEvents:UIControlEventValueChanged]; self.refreshControl = refreshControl; 

The above implements a nice liquid-drop animation as part of a native widget.

As UICollectionViewController is a "more evolved" UITableViewController one would expect somewhat of a parity of features, but I can't find a reference anywhere to a built-in way to implement this.

  1. Is there a simple way to do this that I'm overlooking?
  2. Can UIRefreshControl be used somehow with UICollectionViewController despite the header and docs both stating that it's meant to be used with a table view?
like image 863
mjh Avatar asked Oct 26 '12 11:10

mjh


People also ask

How do I refresh UICollectionView?

Since iOS 10, the UITableView and UICollectionView classes have a refreshControl property. You can add a refresh control to a table or collection view by assigning an instance of the UIRefreshControl class to this property.

What is swift UICollectionView?

An object that manages an ordered collection of data items and presents them using customizable layouts.


1 Answers

The answers to both (1) and (2) are yes.

Simply add a UIRefreshControl instance as a subview of .collectionView and it just works.

UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; [refreshControl addTarget:self action:@selector(startRefresh:)     forControlEvents:UIControlEventValueChanged]; [self.collectionView addSubview:refreshControl]; 

That's it! I wish this had been mentioned in the documentation somewhere, even though sometimes a simple experiment does the trick.

EDIT: this solution won't work if the collection is not big enough to have an active scrollbar. If you add this statement,

self.collectionView.alwaysBounceVertical = YES; 

then everything works perfectly. This fix taken from another post on the same topic (referenced in a comment in the other posted answer).

like image 117
mjh Avatar answered Sep 22 '22 16:09

mjh