Pull-to-refresh in UICollectionViewController

mjh picture mjh · Oct 26, 2012 · Viewed 32.7k times · Source

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?

Answer

mjh picture mjh · Oct 27, 2012

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).