swiftswiftuiscrollviewhstack

Snap positioning for cells in SwiftUI Horizontal ScrollView


I have a horizontal scroll with some fixed size content cells, how can I make it so that it's a swipe to go to the next/previous cell? Similar to a tinder swipe but all remain in the scroll view. I want the cells to 'click' to the leading edge of the Hstack

struct ContentView: View {
        let content = ["Hello", "World", "How", "Are", "You"]
        
        var body: some View {
            ScrollView(.horizontal) {
                HStack {
                    ForEach(content) { str in
                        Text(str)
                            .frame(width: 200, height: 200, alignment: .center)
                            .background(Color.green)
                    }
                }
            }
        }
    }

Solution

  • Try applying .scrollTargetLayout() to the HStack and .scrollTargetBehavior(.paging) to the ScrollView. Also:

    @State private var pageId: Int?
    
    var body: some View {
        ScrollView(.horizontal) {
            HStack(spacing: 0) {
                ForEach(Array(content.enumerated()), id: \.offset) { offset, str in
                    Text(str)
                        .frame(height: 200, alignment: .center)
                        .frame(maxWidth: .infinity)
                        .background(
                            RoundedRectangle(cornerRadius: 10)
                                .fill(.green)
                        )
                        .padding(.horizontal, 10)
                        .containerRelativeFrame(.horizontal)
                }
            }
            .scrollTargetLayout()
        }
        .scrollPosition(id: $pageId)
        .scrollTargetBehavior(.paging)
        .onChange(of: pageId) { oldVal, newVal in
            if let newVal {
                print("page \(newVal) selected")
            }
        }
    }
    

    Animation