14

I am trying to implement a UICollectionViewFlowLayout or UICollectionViewLayout - any ways the layoutAttributesForItem is never called.

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

Like this flowout example:

I have created a Playground project you can look at. Overall I am just trying to scale up the center view.

Chris G.
  • 23,930
  • 48
  • 177
  • 302
  • I know it's a very trivial question but I dare ask it. Have you assigned your custom class to the layout of your collectionView correctly? – Adeel Miraj Oct 19 '16 at 11:33
  • Thanks for asking :-) If you have the time, have a look at this Playground: https://github.com/JCzz/CollectionView Overall I am just trying to scale the View in the center of the screen. – Chris G. Oct 19 '16 at 11:42
  • @ChrisG.I'm having the same problem. Everything seems fine except "LayoutAttributesForItem" does not fire at all. I'm not sure why. – jnel899 Jan 10 '17 at 16:41
  • I never found the solution - let me know if you do. I found that the documentation could be better. – Chris G. Jan 11 '17 at 10:32
  • Has anyone here figured out any solution around this issue? – Mohammad Abdurraafay Jan 22 '18 at 16:58

1 Answers1

7

Try to use it by Sub-Classing 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
    }

}
kalpesh jetani
  • 1,775
  • 19
  • 33