0
我在具有兩個集合視圖的視圖控制器中使用sizeForItemAtIndexPath
函數。SizeForItemAtIndexPath:獲取項目標準大小
如何排除sizeForItemAtIndexPath
函數中的一個集合視圖,或者如何在不更改任何內容的情況下獲取原始單元大小?
我在具有兩個集合視圖的視圖控制器中使用sizeForItemAtIndexPath
函數。SizeForItemAtIndexPath:獲取項目標準大小
如何排除sizeForItemAtIndexPath
函數中的一個集合視圖,或者如何在不更改任何內容的情況下獲取原始單元大小?
排除一個集合視圖
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// to exclude one collection view
if collectionView == collectionViewOne {}// first
if collectionView.tag == number(you set before){}// second
// to get the original cell size without changing anything
//let yourSize = collectionViewLayout.itemSize
//return collectionViewLayout.itemSize // if you have set it globally,
/*
But you can not get it, because itemSize is a property of UICollectionViewFlowLayout.
And you can't cast collectionViewLayout to UICollectionViewFlowLayout Class which is kind of UICollectionViewLayout Class .
Thats not how inheritance works.
*/
}
得到原始細胞大小不做改變
let indexPath = IndexPath(item: number(you choose), section: number(you choose, 0 or 1))
let layout = UICollectionViewFlowLayout() // you set
// got the size
// more generally, if you have set layout per item through delegate.
let size = self.collectionView(collectionViewOne, layout: layout, sizeForItemAtIndexPath: indexPath)
// or try this ,if you have configured globally
let itemSize = layout.itemSize
假設您使用故事板,您可以將每個收藏視圖作爲視圖控制器的出口連接。例如
@IBOutlet weak var collectionViewOne: UICollectionView!
@IBOutlet weak var collectionViewTwo: UICollectionView!
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView == collectionViewOne {
// Do nothing, and set default size.
return layout.itemSize
} else {
//Set size here along with other setup.
return CGSize(width: 150, height: 150)
}
}
我不知道爲什麼,但我無法訪問layout.itemSize,它表示「使用未解析的標識符」佈局「。」 –
解答已更新。 – dengApro
謝謝你,這個作品完美! –