How to make a Horizontal List in SwiftUI?

2020-03-12 07:12发布

问题:

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:

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

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


回答2:

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)


标签: ios swiftui