如何隐藏UICollectionView下的Refresh Control

How to hide Refresh Control under UICollectionView

我正在使用:

我的 CollectionView 代码非常简单:

@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
   super.viewDidLoad()
   let refreshControl = UIRefreshControl()
   refreshControl.addTarget(self, action: #selector(onRefresh), for: .valueChanged)
   refreshControl.isUserInteractionEnabled = false

   collectionView.alwaysBounceVertical = true
   collectionView.refreshControl = refreshControl
}

@objc private func onRefresh(refreshControl: UIRefreshControl) {
   refreshControl.endRefreshing()
}

在我拉动 collectionViewrefreshControl 会出现,如果我滚动 collectionView 回到顶部,然后 refreshControl 会被绘制在单元格上:

我找到了这个案例的解决方案:

refreshControl.layer.zPosition = -1

但现在 Refresh Controll 位于单元格后面并通过单元格之间的空间查看:

我想要得到的结果非常简单和合乎逻辑。我假设它适用于 TableView,因为其中的单元格具有背景填充。为什么 Apple 最初不支持这种方法 CollectionView:

你只需要在refreshcontrol的实例设置好之后添加:

        refreshControl.backgroundColor = UIColor.white
        refreshControl.tintColor = UIColor.black

希望对你有所帮助~