cenkbilgen/AdaptiveGridLayout
Adaptive vertical grid layout for SwiftUI
1. Layout Example
import SwiftUI
import AdaptiveGridLayout
struct SampleView: View {
let itemHeight: CGFloat = 50
@State var items: [(width: CGFloat, color: Color)] = (0..<40).map { _ in
(CGFloat.random(in: 10..<120),
Color(hue: .random(in: 0..<1.0),
saturation: .random(in: 0.1..<0.8),
brightness:.random( in: 0.5..<0.9))
)
}
var body: some View {
AdaptiveVGrid(spacing: 2) { // <------------
ForEach(items.indices, id: \.self) { index in
Rectangle()
.fill(items[index].color)
.frame(width: items[index].width, height: itemHeight)
.overlay { Text(index, format: .number) }
}
}
.border(.blue)
.containerRelativeFrame(.vertical)
}
}<img width="613" alt="Adaptive Layout Sample 1" src="https://github.com/cenkbilgen/AdaptiveGridLayout/assets/6772018/56039db3-ea54-4b51-8ec1-672328957ac4">
Animation
Elements handle their own animation. The easiest way to add some animation to adding and removing elements is with matchedGeometry. For the sample above, add .matchedGeometryEffect(id: fruit.id, in: namespace) to each element then .animation(.spring, value: model.fruits).
[looping_output]
Package Metadata
Repository: cenkbilgen/AdaptiveGridLayout
Stars: 7
Forks: 0
Open issues: 0
Default branch: main
Primary language: swift
License: BSD-3-Clause
README: README.md