Ios6 在UICollectionViewController中拖动以刷新

Ios6 在UICollectionViewController中拖动以刷新,ios6,uikit,pull-to-refresh,Ios6,Uikit,Pull To Refresh,我想在iOS 6下的UICollectionViewController中实现下拉刷新。使用UITableViewController很容易实现这一点,如下所示: UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; [refreshControl addTarget:self action:@selector(startRefresh:) forControlEvents:UIControlEventVa

我想在iOS 6下的
UICollectionViewController
中实现下拉刷新。使用
UITableViewController
很容易实现这一点,如下所示:

UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(startRefresh:)
    forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
上面实现了一个漂亮的液滴动画,作为本机小部件的一部分

由于
UICollectionViewController
是一种“更先进的”
UITableViewController
,人们可能会期望在某种程度上具有相同的功能,但我在任何地方都找不到实现这一功能的内置方法的参考

  • 有没有一个简单的方法可以做到这一点,我忽略了
  • UIRefreshControl
    是否可以与
    UICollectionViewController
    一起使用,尽管标题和文档都声明它要与表视图一起使用
  • (1)和(2)的答案都是肯定的

    只需添加一个
    UIRefreshControl
    实例作为
    .collectionView
    的子视图,它就能正常工作

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(startRefresh:)
        forControlEvents:UIControlEventValueChanged];
    [self.collectionView addSubview:refreshControl];
    
    就这样!我希望在文档中的某个地方提到过这一点,即使有时候一个简单的实验就可以做到

    编辑:如果集合不够大,无法有活动的滚动条,则此解决方案将不起作用。如果你加上这句话,

    self.collectionView.alwaysBounceVertical = YES;
    

    然后一切都完美地工作。此修复来自同一主题(在另一个发布的答案中的评论中引用)。

    mjh的答案是正确的

    我遇到了这样一个问题:如果
    collectionView.contentSize
    的大小不大于
    collectionView.frame.size
    ,则无法滚动
    collectionView
    。您也不能设置
    contentSize
    属性(至少我不能)

    如果它不能滚动,它将不允许您执行拉刷新

    我的解决方案是将
    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);
    }
    
    -(CGSize)集合视图内容大小
    {
    CGFloat高度=[super collectionViewContentSize]。高度;
    //始终返回比帧大的contentSize,以便它可以滚动,UIRefreshControl将正常工作
    if(高度
    我一直在寻找同样的解决方案,但是Swift。根据上述答案,我做了以下工作:

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

    refreshCtrl.endRefreshing()
    

    我正在使用故事板并设置self.collectionView.alwaysBounceVertical=YES不起作用。选择
    反弹
    垂直反弹
    可以帮我完成这项工作


    从iOS 10开始,
    refreshControl
    属性现在已添加到
    UIScrollView
    ,因此您可以直接在集合视图上设置刷新控件


    我必须指出UIRefreshControl.alloc.init是对点表示法的滥用。点表示法表示访问内部状态,而括号表示法表示希望对象执行某些操作。将其视为延迟加载,其中访问的内部状态是一个初始化实例。有点夸张,但这比我最初的回答要好,我最初的回答是:p:)如果要在这里使用点符号,我更喜欢UIRefreshControl。new@JuanGonzález:将控件设置为iVar,ala
    \u refreshControl=blah
    ,然后在
    startRefresh:
    ,完成工作后,执行
    \u refreshControl endRefreshing]。如果有人感兴趣并使用Swift,我为UICollectionViewController编写了一个简单的快速扩展,以方便刷新控件。您还可以执行self.collectionView.alwaysBounceVertical=YES。这是值得赞扬的。
    
    UIRefreshControl *refreshControl = [UIRefreshControl new];
    [refreshControl addTarget:self action:@selector(refreshControlAction:) forControlEvents:UIControlEventValueChanged];
    self.collectionView.refreshControl = refreshControl;