Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/edonv/compositionallayoutbuilder
A Result Builder for CollectionView CompositionLayouts!
https://github.com/edonv/compositionallayoutbuilder
collectionview compositional-layouts resultbuilder swift
Last synced: 9 days ago
JSON representation
A Result Builder for CollectionView CompositionLayouts!
- Host: GitHub
- URL: https://github.com/edonv/compositionallayoutbuilder
- Owner: edonv
- License: mit
- Created: 2024-02-24T23:41:16.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-03-21T15:54:53.000Z (10 months ago)
- Last Synced: 2024-04-23T23:47:10.035Z (8 months ago)
- Topics: collectionview, compositional-layouts, resultbuilder, swift
- Language: Swift
- Homepage: https://swiftpackageindex.com/edonv/CompositionalLayoutBuilder//documentation/compositionallayoutbuilder
- Size: 32.2 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CompositionalLayoutBuilder
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fedonv%2FCompositionalLayoutBuilder%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/edonv/CompositionalLayoutBuilder)
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fedonv%2FCompositionalLayoutBuilder%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/edonv/CompositionalLayoutBuilder)`CompositionalLayoutBuilder` gives you the ability to "compose" a `UICollectionViewCompositionalLayout`/`NSCollectionViewCompositionalLayout` via result builders syntax.
It can be used in any context you might use a `UICollectionView`/`NSCollectionView`, including another package of mine: [`CollectionView`](https://github.com/edonv/CollectionView) (which will come pre-packaged with initializers that use the layout builders).
## Install
To add `CompositionalLayoutBuilder` to an Xcode project, go to `File` > `Add Package Dependencies...`, and paste `https://github.com/edonv/CompositionalLayoutBuilder` into the Search field.
To add it manually to a Swift Package, add the following to the `dependencies` property in the `Package.swift` file:
```swift
.package(
url: "https://github.com/edonv/CompositionalLayoutBuilder",
upToNextMajor(from: "0.0.0")
)
```## Comparison
With `CompositionalLayoutBuilder`:
```swift
let layout: CompositionalLayout = {
CompositionalLayout {
CompositionalSection {
CompositionalGroup(.horizontal, width: .fractionalWidth(1), height: .absolute(200)) {
LayoutGroup(.vertical, width: .fractionalWidth(0.75), height: .fractionalHeight(1)) {
LayoutItem(width: .fractionalWidth(1), height: .fractionalHeight(0.5))
.repeating(count: 2)
}
.interItemSpacing(.fixed(8))
CompositionalItem(width: .fractionalWidth(0.25), height: .fractionalHeight(1))
}
.interItemSpacing(.fixed(16))
}CompositionalSection {
CompositionalGroup(.horizontal, width: .fractionalWidth(1), height: .absolute(100)) {
CompositionalItem(width: .fractionalWidth(0.5), height: .fractionalHeight(1))
}
}
.orthogonalScrollingBehavior(.continuous)
}
.scrollDirection(.vertical)
}()
```With vanilla `UICollectionViewCompositionalLayout`:
```swift
let layout: UICollectionViewCompositionalLayout = {
UICollectionViewCompositionalLayout { section, _ in
switch section {
case 0:
let innerItem = NSCollectionLayoutItem(layoutSize: .init(
widthDimension: .fractionalWidth(1),
heightDimension: .fractionalHeight(0.5)
))
let innerGroup = NSCollectionLayoutGroup.vertical(
layoutSize: .init(
widthDimension: .fractionalWidth(0.75),
heightDimension: .fractionalHeight(1)
),
subitem: innerItem,
count: 2
)
innerGroup.interItemSpacing = .fixed(8)
let outerItem = NSCollectionLayoutItem(layoutSize: .init(
widthDimension: .fractionalWidth(0.25),
heightDimension: .fractionalHeight(1)
))
let mainGroup = NSCollectionLayoutGroup.horizontal(
layoutSize: .init(
widthDimension: .fractionalWidth(1),
heightDimension: .absolute(200)
),
subitems: [innerGroup, outerItem]
)
return .init(group: mainGroup)
case 1:
let innerItem = NSCollectionLayoutItem(layoutSize: .init(
widthDimension: .fractionalWidth(0.5),
heightDimension: .fractionalHeight(1)
))
let mainGroup = NSCollectionLayoutGroup.horizontal(
layoutSize: .init(
widthDimension: .fractionalWidth(1),
heightDimension: .absolute(100)
),
subitems: [innerItem]
)
let section = NSCollectionLayoutSection(group: mainGroup)
section.orthogonalScrollingBehavior = .continuous
return section
default:
return nil
}
}
}()
```## Documentation
Documentation can be found [here, hosted on Swift Package Index](https://swiftpackageindex.com/edonv/CompositionalLayoutBuilder//documentation/compositionallayoutbuilder).
## To-Do's
- [x] Add equivalent UIKit code to README for comparison.