我有一个collectionView(1),我正在其上设置标题。此标头在另一个collectionView(2)内部。现在,我尝试根据标头中collectionView(2)的大小在referenceSizeForHeaderInSection中设置headerSize。我基本上被困住了,不知道从这里去哪里,我尝试了一切。我可以通过以下方式获取标题高度:
collectionView.collectionViewLayout.collectionViewContentSize.height
,但仅在collectionView(2)中设置公共变量。试图获取公共变量,它将在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)
}
答案 0 :(得分:0)
假设您有collectionView1和collectionView2,则只需执行if检查,就可以在委托/数据源方法中访问其中之一。
例如,在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
}
}
注意: :要使用collectionView1和collectionView2,您需要链接出口