I can wrap all my views inside a List
List { // contents }
But this seems to be vertical scrolling. How do I make it horizontal?
The horizontal list can be created by simply putting HStack inside the ScrollView. This will create a scroll view with height 190, and HStack inside it.
Probably the simplest way to build a list is to create a new SwiftUI view and wrap the Hello World text in a List: struct StaticListView: View { var body: some View { List { Text("Hello, world!") } } } To add more items to the list, we can just add another line: List { Text("Hello, world!") Text("Hello, SwiftUI!") }
You need to add .horizontal
property to the scrollview. otherwise it won't scroll.
ScrollView (.horizontal, showsIndicators: false) { HStack { //contents } }.frame(height: 100)
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) } } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With