是否可以在CollectionView的标头中放置刷新控制器



我有一个带有HeaderCollectionView,我想在CollectionView中添加一个RefreshController,我已经这样做了。正如我所期望的那样,它在Header的上方显示。但是,我希望RefreshControllerHeader中显示<strong]。>

原因是Toolbar正在覆盖Header。当我下拉刷新时,Header会以我希望RefreshController可见的方式显示。但我必须下拉Header的高度才能使RefreshController可见,这是
a(距离太大,无法滚动,
b(RefreshControllerCollectionView之间的空间太大

这里有一些代码:

var refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()

[...]

refreshControl.tintColor = .gray
refreshControl.backgroundColor = .white
refreshControl.addTarget(self, action: #selector(refreshContents), for: .valueChanged)
eventCollectionView.refreshControl = refreshControl
eventCollectionView.register(EventHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: toolbarId)
}
@objc func refreshContents() {
self.perform(#selector(finishedRefreshing), with: nil, afterDelay: 1.5)
}

@objc func finishedRefreshing() {
refreshControl.endRefreshing()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return .init(width: screenWidth, height: 50)
}

编辑:问这个问题的时候我还不知道contentInset。通过使用拉里·皮克尔斯建议的contentInset,我能够获得所需的结果。

正如我的Larry Pickles在评论中所建议的那样,我想要的可以通过将collectionViewcontentInset顶部设置为50来完成。这样,我甚至不必将RefreshController放入Header中。

最新更新