Question
How to make a Horizontal List in SwiftUI?
I can wrap all my views inside a List
List {
// contents
}
But this seems to be vertical scrolling. How do I make it horizontal?
46 41131
46
Question
I can wrap all my views inside a List
List {
// contents
}
But this seems to be vertical scrolling. How do I make it horizontal?
Solution
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)
}
}
Solution
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)
}
}
}
}