I just can´t find out how to get access to custom layout attributes in the apply() method of a custom cell.
I have to implement a custom layout attribute to my CollectionViewLayoutAtrributes
, so I subclassed them. This works well so far:
class TunedLayoutAttributes: UICollectionViewLayoutAttributes {
var customLayoutAttributeValue: CGFloat = 1000
override func copy(with zone: NSZone?) -> Any {
let copy = super.copy(with: zone) as! TunedLayoutAttributes
customLayoutAttributeValue = customLayoutAttributeValue
return copy
}
override func isEqual(_ object: Any?) -> Bool {
if let attributes = object as? TunedLayoutAttributes {
if attributes. customLayoutAttributeValue == customLayoutAttributeValue {
return super.isEqual (object)
}
}
return false
}
}
The value has to dynamically change based on user scroll interaction.
Now I need my custom cells to update their appearance after an invalidateLayout
call from the custom UICollectionViewLayout
class. To my knowledge this usually can also be done by overriding the cells classes apply(_ layoutAttributes: UICollectionViewLayoutAttributes)
method.
Usually like so:
override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
let newFrame = // calculations here. CGRect(....)
layoutAttributes.frame = newFrame
}
Now the missing chemistry:
Unlike in the apply()
example above my new customLayoutAttributeValue
is (of course?) not part of the layoutAttributes:
in the method.
So I tried to downcast the layoutAttributes to my custom class like so:
override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
let tunedLayoutAttributes = layoutAttributes as! TunedLayoutAttributes
tunedLayoutAttributes.customLayoutAttributeValue = // calculation here.
}
So how do I get access to tunedLayoutAttributes in the apply() method? Any help is appreciated. Thanks for reading!