显示collectionView的标头大小,基于该标头内部的集合视图动态内容不起作用

问题描述 投票:0回答:1

我有一个collectionView(1),我正在设置一个标题。此标头包含另一个collectionView(2)。现在我尝试根据标题中collectionView(2)的大小在referenceSizeForHeaderInSection中设置headerSize。我基本上卡住了,不知道从哪里开始,我尝试了一切。我能够得到标题高度:

collectionView.collectionViewLayout.collectionViewContentSize.height

但只能在collectionView(2)中设置一个公共变量。试图获取公共var它将无法在collectionView(1)referenceSizeForHeaderInSection中工作(仅当我使用DispatchQueue时,它无法获得标头的返回值)。可能我做错了什么。需要帮助,另一个方向,任何它的赞赏。

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

    let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "VideoHeader", for: indexPath)
    header.removeFromSuperview()
    header.backgroundColor = UIColor.white

    headerAdvertCollection.collectionView.translatesAutoresizingMaskIntoConstraints = false
    header.addSubview(headerAdvertCollection.collectionView)
    headerAdvertCollection.collectionView.centerXAnchor.constraint(equalTo: header.centerXAnchor).isActive = true
    headerAdvertCollection.collectionView.centerYAnchor.constraint(equalTo: header.centerYAnchor).isActive = true
    headerAdvertCollection.collectionView.heightAnchor.constraint(equalTo: header.heightAnchor).isActive = true

    headerAdvertCollection.collectionView.widthAnchor.constraint(equalTo: header.widthAnchor, constant: -10).isActive = true

    headerAdvertCollection.setupCollectionView()

    return header
}

//MARK: Collection View Header Height
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

    let advertisingView : CGFloat = 450
    let height : CGFloat = advertisingView

    return CGSize(width: collectionView.frame.width - 10, height: height)
}
swift swift3 uicollectionview uicollectionviewcell uicollectionviewlayout
1个回答
0
投票

假设您有collectionView1和collectionView2,只需执行if检查即可在delegate / datasource方法中访问其中一个。

例如,在referenceSizeForHeaderInSection中:

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

   if collectionView == collectionView1{
     let advertisingView : CGFloat = 450
     let height : CGFloat = advertisingView

     //supposing you want to use collectionView2
     return CGSize(width: collectionView2.frame.width - 10, height: height)
   }else{
     // HERE YOU CAN RETURN A VALUE FOR collectionView2
   }

}

注意:要使用collectionView 1和collectionView 2,您需要链接插座

© www.soinside.com 2019 - 2024. All rights reserved.