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 , for example:

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

The above implements a nice animation with liquid in the form of a partial widget.

Since the UICollectionViewController is a “more developed” UITableViewController , one would expect some parity of functions, but I cannot find the link anywhere in the built-in way to implement this.

  • Is there an easy way to do this that I am missing?
  • Can the UIRefreshControl be UIRefreshControl some way with the UICollectionViewController , despite the title and documents stating that they are intended for use in the table view?
+72
uikit ios6 pull-to-refresh
Oct 26 '12 at 11:00
source share
5 answers

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

Just add an instance of UIRefreshControl as a subtask .collectionView and it just works.

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

What is it! I would like this to be mentioned somewhere in the documentation, although sometimes a simple experiment does the trick.

EDIT: This solution will not work if the collection is not large enough to have an active scrollbar. If you add this statement,

 self.collectionView.alwaysBounceVertical = YES; 

then everything works fine. This fix is ​​taken from another post on the same topic (link in the comment in another answer).

+213
Oct 26 '12 at 22:52
source share

I was looking for the exact same solution, but in Swift. Based on the answer above, I did the following:

 let refreshCtrl = UIRefreshControl() ... refreshCtrl.addTarget(self, action: "startRefresh", forControlEvents: .ValueChanged) collectionView?.addSubview(refreshCtrl) 

Do not forget:

 refreshCtrl.endRefreshing() 
+18
Jan 07 '15 at
source share

I used Storyboard, and setting self.collectionView.alwaysBounceVertical = YES; did not work. Choosing Bounces and Bounces Vertically does the job for me.

enter image description here

+8
Apr 19 '15 at 13:46 on
source share
Answer

mjh is correct.

I ran into a problem where, if collectionView.contentSize no bigger than collectionView.frame.size , you cannot miss the scroll of collectionView . You also cannot set the contentSize property (at least I couldn't).

If it cannot scroll, it will not let you try to update.

My solution was to subclass UICollectionViewFlowLayout and override the method:

 - (CGSize)collectionViewContentSize { CGFloat height = [super collectionViewContentSize].height; // Always returns a contentSize larger then frame so it can scroll and UIRefreshControl will work if (height < self.collectionView.bounds.size.height) { height = self.collectionView.bounds.size.height + 1; } return CGSizeMake([super collectionViewContentSize].width, height); } 
+2
Mar 27 '13 at 17:01
source share

Now the refreshControl property has refreshControl added to UIScrollView with iOS 10, so you can set the update control directly in the collection views.

https://developer.apple.com/reference/uikit/uiscrollview/2127691-refreshcontrol

 UIRefreshControl *refreshControl = [UIRefreshControl new]; [refreshControl addTarget:self action:@selector(refreshControlAction:) forControlEvents:UIControlEventValueChanged]; self.collectionView.refreshControl = refreshControl; 
+1
Nov 04 '16 at 0:44
source share



All Articles