Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ofthewolf/anylayout
Create dynamic and modern collections effortlessly without relying on enums with AnyLayout.
https://github.com/ofthewolf/anylayout
compositional-layouts ios type-erasure ui-components uicollectionviewcompositionallayout uikit
Last synced: 3 days ago
JSON representation
Create dynamic and modern collections effortlessly without relying on enums with AnyLayout.
- Host: GitHub
- URL: https://github.com/ofthewolf/anylayout
- Owner: OfTheWolf
- License: mit
- Created: 2024-05-02T23:45:49.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-05-03T14:11:08.000Z (8 months ago)
- Last Synced: 2024-11-08T05:20:33.050Z (about 2 months ago)
- Topics: compositional-layouts, ios, type-erasure, ui-components, uicollectionviewcompositionallayout, uikit
- Language: Swift
- Homepage: https://link.medium.com/mjWDs6HLiJb
- Size: 163 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AnyLayout
Create dynamic and modern collections effortlessly without relying on enums with **AnyLayout**.
**AnyLayout** simplifies building UI collections using `UICollectionViewCompositionalLayout` with type erasure for flexibility.
## Demo
## Installation
Integrate AnyLayout into your project using Swift Package Manager:
**Xcode > File > Add Package Dependencies.. :** https://github.com/OfTheWolf/AnyLayout.git
## Getting Started
1. Inherit from `CollectionViewController`.
```swift
class ViewController: CollectionViewController {}
```2. Define sections conforming to `SectionProviding`. Implement the `layout` method to configure the section layout.
```swift
struct ListSection: SectionProviding {
let id = UUID()
// Define section layout here
func layout(_ sectionIndex: Int, _ layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection {
// Configure section layout
}
}
```3. Define items conforming to `ItemProviding`.
```swift
struct TextItem: ItemProviding {
let text: String
}
```4. Implement cell registration and dequeueing.
```swift
extension TextCell: Registering {
typealias Cell = TextCell
typealias Item = TextItem/// Boilerplate code
static var registration: UICollectionView.CellRegistration = UICollectionView.CellRegistration { cell, indexPath, itemIdentifier in
let data: Item = itemIdentifier.resolve()
cell.configure(with: data)
}
}extension TextCell: Dequeueing {
/// Boilerplate code
static func dequeueCell(_ collectionView: UICollectionView, indexPath: IndexPath, item: AnyItem) -> UICollectionViewCell {
return collectionView.dequeueConfiguredReusableCell(using: Self.registration, for: indexPath, item: item)
}
}
```5. Register your cell types with their corresponding item types.
```swift
// MARK: - Override
override func registerCells(for collectionView: UICollectionView) {
collectionView.register(cell: ImageCell.self, item: ImageItem.self)
collectionView.register(cell: TextCell.self, item: TextItem.self)
}
```6. Prepare the snapshot with your data.
```swift
private func loadData() {
var snap = NSDiffableDataSourceSnapshot()
Mock.data.forEach { data in
snap.appendSections([data.section.eraseToAnySection])
snap.appendItems(data.items.map(\.eraseToAnyItem))
}
dataSource.apply(snap)
}
```7. Build and run your project.
Start building your dynamic UI collections with ease using AnyLayout!