UICollectionViewLayout and method layoutAttributesForItem is never called
Asked Answered
K

1

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.

Kala answered 19/10, 2016 at 11:6 Comment(5)
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?Hemeralopia
Thanks for asking :-) If you have the time, have a look at this Playground: github.com/JCzz/CollectionView Overall I am just trying to scale the View in the center of the screen.Kala
@ChrisG.I'm having the same problem. Everything seems fine except "LayoutAttributesForItem" does not fire at all. I'm not sure why.Hendershot
I never found the solution - let me know if you do. I found that the documentation could be better.Kala
Has anyone here figured out any solution around this issue?Rafaelarafaelia
O
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
    }

}
Openhearted answered 23/2, 2017 at 9:55 Comment(5)
So what you really proposing here is that we call layoutAttributesForItem(at:) ourselves instead of relying the UICollectionViewFlowLayout and expect it to callback? :/Rafaelarafaelia
@MohammadAbdurraafay yes, also if we are using custom flowlayout then it will call this override func.Openhearted
This is the best example on the Internet (that I can find) of layoutAttributesForItem in swift.High
It's not a solution anyway, but subclassing FlowLayout didn't change anything.Goiter
@Goiter check out my solution, hope it helps.Linebreeding

© 2022 - 2024 — McMap. All rights reserved.