【问题标题】:UICollectionViewCells hiding behind headerViewUICollectionViewCells 隐藏在 headerView 后面
【发布时间】:2021-08-23 06:32:47
【问题描述】:

将 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)
   
}

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewcell


    【解决方案1】:
    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)
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-03
      • 2020-01-16
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-31
      • 1970-01-01
      相关资源
      最近更新 更多