Skip to content

Instantly share code, notes, and snippets.

@benigumocom
Last active April 24, 2024 06:03
Show Gist options
  • Save benigumocom/29ca624837d5fdf0f025c6b9df0b41fa to your computer and use it in GitHub Desktop.
Save benigumocom/29ca624837d5fdf0f025c6b9df0b41fa to your computer and use it in GitHub Desktop.
【SwiftUI】ScrollView や List を snap する 👉 https://android.benigumo.com/20240424/snap-list/
import SwiftUI
struct SnapList: View {
private let colors: [Color] = [
.white, .black, .red, .blue, .yellow, .green
]
var body: some View {
ScrollView {
LazyVStack(spacing: 0) {
ForEach(0..<30, id: \.self) { i in
colors[i % 6]
.frame(height: 100)
}
}
.scrollTargetLayout() // *
}
.scrollTargetBehavior(.viewAligned) // *
.frame(width: 300, height: 300)
.border(.gray)
}
}
#Preview {
SnapList()
}
@benigumocom
Copy link
Author

sc 2024-04-24 at 13 54 41

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment