SwiftUI scroll to horizontal list item - Stack Overflow

PHOTO EMBED

Mon Mar 20 2023 13:30:08 GMT+0000 (Coordinated Universal Time)

Saved by @Raihan #swift #swiftui

ScrollViewReader { proxy in
    ScrollView(.horizontal, showsIndicators: false) {
        HStack { // or LazyHStack
            ForEach(0..<items.count) { index in
                Text(items[index].title)
                    .id(index)
                    .onTapGesture {
                        print("tapped \(index)")
                    }
            }
        }
        .onAppear {
            DispatchQueue.main.async { 
                proxy.scrollTo(selectedIndex, anchor: .center)
            }
        }
    }
}
content_copyCOPY

https://stackoverflow.com/questions/69279513/swiftui-scroll-to-horizontal-list-item