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.
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.
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.
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.
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.
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)
}
}
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With