ios - UICollectionViewCells 隐藏在 headerView 后面
问题描述
将 UICollectionView 与自定义标题一起使用。奇怪的事情是隐藏在标题单元格后面的自定义单元格。下面是代码:
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "CalendarHeaderView", for: indexPath) as! CalendarHeaderView
switch kind {
case UICollectionView.elementKindSectionHeader:
reusableview.frame = CGRect(x: 0 , y: 0, width: self.view.frame.width, height: 40)
//do other header related calls or settups
reusableview.headerLabel.text = "Month"
return reusableview
default: fatalError("Unexpected element kind")
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.bounds.width / 7.0
let height = width
return CGSize(width: width, height: height)
}
解决方案
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionView.elementKindSectionHeader:
/// PROBLEM
reusableview.frame = CGRect(x: 0 , y: 0, width: self.view.frame.width, height: 40)
return reusableview
default: fatalError("Unexpected element kind")
}
}
您不应该设置UICollectionView.elementKindSectionHeader
. UICollectionView
为您完成所有这些。您只需要告诉它需要多大 ( CGSize
)。
您可以UICollectionView
通过以下方法了解此尺寸 -
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.bounds.width, height: 40)
}
}
推荐阅读
- bash - Perl $ENV 不访问环境变量
- android - 连接到具有最佳 rssi 的蓝牙设备
- docker - 使用 Docker Hub 映像作为 Azure DevOps 发布管道的工件
- c# - 是否需要为委托创建新实例?
- javascript - 计算对象数组中的相同值
- android - Kotlin 材料设计对话框获取选定选项
- python - Web 从某个类的元素中抓取数据?
- python - 计算年份数据的逐月和逐年变化
- html - 从 3.1.0 升级到 3.3.7 时的引导模型问题
- c++ - esp32 arduino 上的分离 std::thread 有时会阻塞,有时不会