我想要一个水平滚动的集合 View ,但我也希望它在顶部和右侧有一个标题。我已经尝试像垂直滚动集合 View 一样实现它,并且标题最终位于第一个单元格的左侧。
这是我如何使 header 出列:
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: identifier, for: indexPath) as! HeaderView
header.setWidth(to: headerWidth)
return header
}
标题大小:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 12)
}
除此之外,我初始化的唯一其他属性是集合 View 的滚动方向和插图
最佳答案
标题位置:在 顶部 在垂直滚动的情况下和 中的部分左 在水平滚动的情况下的部分,因此在您的情况下它显示在部分的左侧是正常的。
如果您想显示您的 reusable view
在每个部分的右侧您可以使用 UICollectionView.elementKindSectionFooter 反而 。
但是如果你想显示你的reusable view
在每个部分之间,您可以将第一部分中标题 View 的大小设置为 .zero 。
您可以使用 referenceSizeForHeaderInSection
在每个标题 View 大小中进行操作功能如下:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
if section == 0 {
return .zero
}
return CGSize(width: collectionView.frame.width, height: 12)
}
在这种情况下,我删除了(技术上没有删除而是隐藏)左侧的第一个标题,它位于第一部分,但是您可以设置每个标题的大小。
关于ios - 如何实现顶部带有标题的水平滚动 Collection View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60428393/