Notice: This component will be deprecated over the next few months in favor of the Cards and List components. See our public tracker for more details on timing and the deprecation plan.
Allows passing layout attributes to the cells and supplementary views.
To add this component to your Xcode project using CocoaPods, add the following to your Podfile
:
pod 'MaterialComponents/CollectionLayoutAttributes'
Then, run the following command:
pod install
Before using Collection Layout Attributes, you'll need to import it:
import MaterialComponents.MaterialCollectionLayoutAttributes
#import "MaterialCollectionLayoutAttributes.h"
The MDCCollectionViewLayoutAttributes
class allows passing properties to a cell from a collection
view layout. Override the -applyLayoutAttributes
method of any UICollectionReusableView
or
UICollectionViewCell
subclasses, then apply any of the properties of the attributes class.
override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
super.apply(layoutAttributes)
if let attr = layoutAttributes as? MDCCollectionViewLayoutAttributes {
if (attr.representedElementCategory == .cell) {
// Example to set a background image to the cell background view.
self.backgroundView = UIImageView(image: attr.backgroundImage)
}
}
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
[super applyLayoutAttributes:layoutAttributes];
if ([layoutAttributes isKindOfClass:[MDCCollectionViewLayoutAttributes class]]) {
MDCCollectionViewLayoutAttributes *attr = (MDCCollectionViewLayoutAttributes *)layoutAttributes;
if (attr.representedElementCategory == UICollectionElementCategoryCell) {
// Example to set a background image to the cell background view.
self.backgroundView = [[UIImageView alloc] initWithImage:attr.backgroundImage];
}
}
}