具有低高度 UICollectionView 的 UIRefreshControl

UIRefreshControl with low height UICollectionView

我有一个视图,其中包含顶部的另一个视图,我用它来显示一些基本信息。它大约占总视图高度的 40%。在 "header" 视图下方,我使用的是可滚动的 UICollectionView。现在,我已将 UIRefreshControl 添加到我的 UICollectionView,但从未发生刷新,因为用户无法将 UICollectionView 下拉那么远。当我降低顶视图的高度时,它开始工作,因为有足够的 space 将 collectionview 拉下来。

这是我添加 refreshControl 的方式:

    self.matchDetailRefreshControl = UIRefreshControl()
    self.matchDetailRefreshControl.addTarget(self, action: #selector(MatchDetailViewController.fetchAll), forControlEvents: .ValueChanged)
    self.collectionView!.addSubview(self.matchDetailRefreshControl)
    self.collectionView!.alwaysBounceVertical = true

看看这张截图以供参考:

如您所见,UIRefreshControl 没有完全填充,而我的手指已经在屏幕底部。

我该如何解决?

你可以实现scrollViewDidScroll。 如果 scrollView 的 contentOffset 超过某个点,则使用 beginRefreshing()

以编程方式实现刷新

例如(刷新控件连接到名为 'refreshControl' 的插座)

func scrollViewDidScroll(scrollView: UIScrollView) {

    let currentOffset = scrollView.contentOffset

    let yOffset = currentOffset.y

    if yOffset < -30.0 && !refreshControl.refreshing {
        refreshControl.beginRefreshing()
    }
}

如果您还没有将 scrollView 的委托设置为 self,请不要忘记

编辑:抱歉是 beginRefreshing(),不是 startRefreshing()。