隐藏在 headerView 后面的 UICollectionViewCells
UICollectionViewCells hiding behind headerView
将 UICollectionView 与自定义 header 结合使用。奇怪的是自定义单元格隐藏在 header 单元格后面。下面是代码:
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)
}
}
将 UICollectionView 与自定义 header 结合使用。奇怪的是自定义单元格隐藏在 header 单元格后面。下面是代码:
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)
}
}