我有一个带有Header
的CollectionView
,我想在CollectionView
中添加一个RefreshController
,我已经这样做了。正如我所期望的那样,它在Header
的上方显示。但是,我希望RefreshController
在Header
中显示<strong]。>
原因是Toolbar
正在覆盖Header
。当我下拉刷新时,Header
会以我希望RefreshController
可见的方式显示。但我必须下拉Header
的高度才能使RefreshController
可见,这是
a(距离太大,无法滚动,
b(RefreshController
和CollectionView
之间的空间太大
这里有一些代码:
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在评论中所建议的那样,我想要的可以通过将collectionView
的contentInset
顶部设置为50来完成。这样,我甚至不必将RefreshController
放入Header
中。