UICollectionViewController中的Pull-to-refresh [英] Pull-to-refresh in UICollectionViewController

查看:128
本文介绍了UICollectionViewController中的Pull-to-refresh的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iOS 6下的 UICollectionViewController 中实现pull-down-to-refresh。这很容易用 UITableViewController实现,如下所示:

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.

作为 UICollectionViewController 是一个更进化的 UITableViewController 有人会期待功能的奇偶性,但我无法在任何地方找到实现此内置方式的参考。

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.


  1. 是否有一个简单的这样做的方式我忽略了吗?

  2. 可以 UIRefreshControl 以某种方式使用 UICollectionViewController 尽管标题和文档都声明它与表视图一起使用?

  1. Is there a simple way to do this that I'm overlooking?
  2. Can UIRefreshControl be used somehow with UICollectionViewController despite the header and docs both stating that it's meant to be used with a table view?


推荐答案

(1)和(2)的答案都是肯定的。

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

只需添加 UIRefreshControl 实例作为 .collectionView 的子视图,它只是有效。

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.

编辑:如果集合不大,这个解决方案将不起作用足以拥有一个活动的滚动条。如果你添加这个陈述,

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).

这篇关于UICollectionViewController中的Pull-to-refresh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆