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.
UIRefreshControl
be used somehow with UICollectionViewController
despite the header and docs both stating that it's meant to be used with a table view?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.
An object that manages an ordered collection of data items and presents them using customizable layouts.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With