Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionViewLayout and method layoutAttributesForItem is never called

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.

like image 623
Chris G. Avatar asked Oct 19 '16 11:10

Chris G.


People also ask

What are uicollectionviewlayout attributes?

Collection view layouts are subclasses of the abstract class UICollectionViewLayout. They define the visual attributes of every item in your collection view. The individual attributes are instances of UICollectionViewLayoutAttributes. These contain the properties of each item in your collection view, such as the item’s frame or transform.

What is layoutattributesfordecorationview in Salesforce?

layoutAttributesForDecorationView (ofKind:at:) (if your layout supports decoration views) These methods provide the fundamental layout information that the collection view needs to place contents on the screen. If your layout doesn’t support supplementary or decoration views, don’t implement the corresponding methods.

How to get layout information from a collection view?

When the collection view needs some layout information, it asks your layout object to provide it by calling certain methods in a specific order: collectionViewContentSize: This method returns the width and height of the collection view’s contents.

What is the use of prepare () method in uicollectionview?

Note: Since prepare () is called whenever the collection view's layout becomes invalid, there are many situations in a typical implementation where you might need to recalculate attributes here. For example, the bounds of the UICollectionView might change when the orientation changes.


Video Answer


2 Answers

So I ran into the same issue and I started exploring this project by apple, I think it's a bug, scrolling to the first item in the collectionview in viewWillAppear solved the issue.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if collectionView.numberOfItems(inSection: 0) > 0 {
        collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .top, animated: false)
    }
}
like image 116
impalairis Avatar answered Sep 23 '22 03:09

impalairis


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
    }

}
like image 7
kalpesh jetani Avatar answered Oct 18 '22 00:10

kalpesh jetani