47

I can wrap all my views inside a List

List {
   // contents
}

But this seems to be vertical scrolling. How do I make it horizontal?

1

4 Answers 4

62

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

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

Update

ScrollView {
    LazyHStack {
         ForEach(arrayOfContent, id: \.id) { item in
              // content
         }
         .listStyle(.plain)
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's not a list it renders it all simultaniously
40

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)
                        }
                }
            }
        }

Comments

15

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)

7 Comments

This would not reuse view-s, so not sure that it is ok solution. Also, i did not see anywhere example of horizontal list, maybe we will need to wait for it a bit :)
I am not sure that even List properly reuses the items )
@Markicevic Apple tutorials also use the same approach to make horizontal scroll content and as DenFav said we're not sure about List reusing. However, certainly there will be improvements in the next beta releases!
Agree that we are not yet sure List is reusing view-s, but not sure how it would be possible for it to not reuse, i mean why have list, we could have Scroll and VStack in it, why then have List. I agree this is correct to have horizontal items on screen, but not sure it will work as List. Still a lot to lear from SwiftUI.
I inspected the hierarchy and it looks like the List is built on top of UITableView, and it recycles cell as well; the HStack within a ScrollView doesn't recycle, if you try to debug hierarchy you'll see all view outside the scroll view bounds
|
1

You can use .horizontal property and use custom elements. For me, I used custom CircleView

  var body: some View {
        VStack{
            Divider()
            ScrollView(.horizontal){
                HStack(spacing:10){
                    ForEach(0..<10){
                        index in
                        CircleView(label: "\(index)")
                    }
                }.padding()
            }.frame(height:100)
            Divider()
            Spacer()
        }
        
    }
}

    //struct CircleView:View
    @State var label:String
    var body:some View {
        ZStack{
            Circle()
                .fill(Color.yellow)
                .frame(width: 70, height: 70)
            Text(label)
        }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.