How to make a Horizontal List in SwiftUI?

Starting from iOS 14 beta1 & XCode 12 beta1 you will be able to wrap LazyHStack in a ScrollView to create a horizontal lazy list of items, i.e., each item will only be loaded on demand:

ScrollView(.horizontal) {
            LazyHStack {
                ForEach(0...50, id: \.self) { index in
                    Text(String(index))
                        .onAppear {
                            print(index)
                        }
                }
            }
        }

You need to add .horizontal property to the scrollview. otherwise it won't scroll.

ScrollView (.horizontal, showsIndicators: false) {
     HStack {
         //contents
     }
}.frame(height: 100)

To make a horizontal scrollable content, you can wrap a HStack inside a ScrollView:

ScrollView {
  HStack {
    ForEach(0..<10) { i in
      Text("Item \(i)")
      Divider()
    }
  }
}
.frame(height: 40)

Tags:

Ios

Swiftui