UICollectionViewLayout and layoutAttributesForItem method are never called

I'm trying to implement a UICollectionViewFlowLayout or UICollectionViewLayout - any ways that layoutAttributesForItem never called.

I see from others that they call layoutAttributesForItem from self.layoutAttributesForItem .

Like this example thread :

I created a Playground project that you can look at. In general, I'm just trying to zoom in on the center.

+7
ios uicollectionviewlayout flowlayout
source share
1 answer

Try using it by subclassing UICollectionViewFlowLayout

 let flowLayout = LeftAgignedFlowLayout() flowLayout.minimumLineSpacing = 18 flowLayout.minimumInteritemSpacing = 8 flowLayout.scrollDirection = .vertical self.collectionViewLayout = flowLayout class LeftAgignedFlowLayout : UICollectionViewFlowLayout { override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { let arr = super.layoutAttributesForElements(in: rect)! return arr.map { atts in var atts = atts if atts.representedElementCategory == .cell { let ip = atts.indexPath atts = self.layoutAttributesForItem(at:ip)! } return atts } } override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { var atts = super.layoutAttributesForItem(at:indexPath)! if indexPath.item == 0 { return atts } if atts.frame.origin.x - 1 <= self.sectionInset.left { return atts } let ipPv = IndexPath(item:indexPath.row-1, section:indexPath.section) let fPv = self.layoutAttributesForItem(at:ipPv)!.frame let rightPv = fPv.origin.x + fPv.size.width + self.minimumInteritemSpacing atts = atts.copy() as! UICollectionViewLayoutAttributes atts.frame.origin.x = rightPv return atts } } 
+1
source share

All Articles