在UICollectionViewController中进行刷新
我想在iOS 6的UICollectionViewController实现下拉刷新。这很容易实现与UITableViewController ,如下所示:
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; [refreshControl addTarget:self action:@selector(startRefresh:) forControlEvents:UIControlEventValueChanged]; self.refreshControl = refreshControl;
上面实现了一个很好的液滴animation作为本地部件的一部分。
由于UICollectionViewController是一个“更进化”的UITableViewController人们会期望有一些UICollectionViewController的function,但我无法find一个引用到任何内置的方式来实现这一点。
- 有没有简单的方法来做到这一点,我俯瞰?
-
UIRefreshControl可以用UIRefreshControl以某种方式使用,尽pipe标题和文档都指出它是用来与表视图?
(1)和(2)的答案是肯定的。
只需添加一个UIRefreshControl实例作为UIRefreshControl的子视图,它只是工作。
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; [refreshControl addTarget:self action:@selector(startRefresh:) forControlEvents:UIControlEventValueChanged]; [self.collectionView addSubview:refreshControl];
而已! 我希望在某个地方的文档中提到过这个问题,尽pipe有时候一个简单的实验可以做到这一点。
编辑:如果集合不够大,有一个活动的滚动条,这个解决scheme将无法正常工作。 如果你添加这个声明,
self.collectionView.alwaysBounceVertical = YES;
那么一切正常。 此修补程序取自同一主题上的另一篇文章 (在其他发布的答案中的评论中引用)。
我正在寻找相同的解决scheme,但在Swift中。 基于上述答案,我做了以下工作:
let refreshCtrl = UIRefreshControl() ... refreshCtrl.addTarget(self, action: "startRefresh", forControlEvents: .ValueChanged) collectionView?.addSubview(refreshCtrl)
不要忘记:
refreshCtrl.endRefreshing()
我正在使用Storyboard并设置self.collectionView.alwaysBounceVertical = YES; 不工作。 selectBounces和Bounces Vertically为我做的工作。

mjh的回答是正确的。
我碰到的问题是,如果collectionView.contentSize不大于collectionView.frame.size ,则无法滚动collectionView 。 你不能设置contentSize属性(至less我不能)。
如果它不能滚动,它不会让你做拉刷新。
我的解决scheme是子类UICollectionViewFlowLayout并覆盖该方法:
- (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); }
refreshControl属性现在已经被添加到iOS 10的UIScrollView ,所以你可以直接在集合视图上设置刷新控件。
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;