【问题标题】:Pull-to-refresh in UICollectionViewControllerUICollectionViewController 中的下拉刷新
【发布时间】:2012-10-26 11:00:22
【问题描述】:

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

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

上面实现了一个不错的液滴动画作为原生小部件的一部分。

由于UICollectionViewController 是“更进化”的UITableViewController,人们可能会期望一些功能相同,但我无法在任何地方找到实现此功能的内置方法的参考。

  1. 有没有我忽略的简单方法?
  2. UIRefreshControl 能否以某种方式与 UICollectionViewController 一起使用,尽管标题和文档都声明它应该与表格视图一起使用?

【问题讨论】:

    标签: ios6 uikit pull-to-refresh


    【解决方案1】:

    (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;
    

    然后一切正常。此修复取自 another post 的同一主题(在其他已发布答案的评论中引用)。

    【讨论】:

    • 我必须指出 UIRefreshControl.alloc.init 是对点符号的滥用。点表示法旨在传达访问内部状态,而括号表示法旨在传达您希望对象执行某些操作。
    • 把它想象成延迟加载,其中被访问的内部状态是一个初始化的实例。有点牵强,但这比我最初的反应要好,它只是“:p”。 :)
    • 如果你要在这里使用点符号,我更喜欢 UIRefreshControl.new
    • @JuanGonzález:将控件设置为 iVar,也就是 _refreshControl = blah,然后在 startRefresh: 中,当工作完成后,执行 _refreshControl endRefreshing];
    • 如果有人感兴趣并使用 Swift,我为 UICollectionViewController 编写了一个快速简单的扩展,以便为 refreshControl 提供一个方便的 var。 gist.github.com/Baza207/f5ff5abf7b8c44e2ffb3
    【解决方案2】:

    我一直在寻找相同的解决方案,但在 Swift 中。根据上面的回答,我做了以下事情:

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

    别忘了:

    refreshCtrl.endRefreshing()
    

    【讨论】:

      【解决方案3】:

      我在使用 Storyboard 并且设置 self.collectionView.alwaysBounceVertical = YES; 不起作用。选择 BouncesBounces Vertically 就可以了。

      【讨论】:

        【解决方案4】:

        自 iOS 10 起,refreshControl 属性现已添加到 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;    
        

        【讨论】:

          【解决方案5】:

          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);
          }
          

          【讨论】:

          • 你也可以self.collectionView.alwaysBounceVertical = YES;。归功于answer
          猜你喜欢
          • 2011-09-29
          • 2013-11-23
          • 1970-01-01
          • 2016-01-08
          • 2012-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-22
          • 2020-10-02
          相关资源
          最近更新 更多