mirror of
https://github.com/material-components/material-components-ios.git
synced 2026-02-20 08:27:32 +08:00
This PR replaces API links containing "/api-docs/" with links to suitable header files in GitHub because the site previously linked to is being taken down and replaced with something that won't handle API docs. Closes https://github.com/material-components/material-components-ios/pull/10045 COPYBARA_INTEGRATE_REVIEW=https://github.com/material-components/material-components-ios/pull/10045 from andrewoverton:replace-mio-api-docs-with-github-links 0d6e56cc11d0c147366a4f6cde829d8b51567ecc PiperOrigin-RevId: 323424362
90 lines
2.7 KiB
Markdown
90 lines
2.7 KiB
Markdown
# Collection Layout Attributes
|
|
|
|
**Notice**: This component will be deprecated over the next few months in favor of the
|
|
[Cards](../Cards) and [List](../List) components. See our
|
|
[public tracker](https://www.pivotaltracker.com/epic/show/3938766) for more details on timing and
|
|
the deprecation plan.
|
|
|
|
---
|
|
|
|
Allows passing layout attributes to the cells and supplementary views.
|
|
|
|
## Design & API Documentation
|
|
|
|
<ul class="icon-list">
|
|
<li class="icon-list-item icon-list-item--link"><a href="https://github.com/material-components/material-components-ios/blob/develop/components/CollectionLayoutAttributes/src/MDCCollectionViewLayoutAttributes.h">API: MDCCollectionViewLayoutAttributes</a></li>
|
|
</ul>
|
|
|
|
- - -
|
|
|
|
## Installation
|
|
|
|
### Installation with CocoaPods
|
|
|
|
To add this component to your Xcode project using CocoaPods, add the following to your `Podfile`:
|
|
|
|
```
|
|
pod 'MaterialComponents/CollectionLayoutAttributes'
|
|
```
|
|
<!--{: .code-renderer.code-renderer--install }-->
|
|
|
|
Then, run the following command:
|
|
|
|
```bash
|
|
pod install
|
|
```
|
|
|
|
- - -
|
|
|
|
## Usage
|
|
|
|
### Importing
|
|
|
|
Before using Collection Layout Attributes, you'll need to import it:
|
|
|
|
<!--<div class="material-code-render" markdown="1">-->
|
|
#### Swift
|
|
```swift
|
|
import MaterialComponents.MaterialCollectionLayoutAttributes
|
|
```
|
|
|
|
#### Objective-C
|
|
```objc
|
|
#import "MaterialCollectionLayoutAttributes.h"
|
|
```
|
|
<!--</div>-->
|
|
|
|
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.
|
|
|
|
<!--<div class="material-code-render" markdown="1">-->
|
|
#### Swift
|
|
```swift
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
```
|
|
#### Objective-C
|
|
```objc
|
|
- (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];
|
|
}
|
|
}
|
|
}
|
|
```
|
|
<!--</div>-->
|