Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Horizontal List in SwiftUI?

Tags:

I can wrap all my views inside a List

List {    // contents } 

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

like image 719
Just a coder Avatar asked Jun 10 '19 08:06

Just a coder


People also ask

How do I make a horizontal list in SwiftUI?

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.

How do I create a list in SwiftUI?

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


2 Answers

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

ScrollView (.horizontal, showsIndicators: false) {      HStack {          //contents      } }.frame(height: 100) 
like image 133
Akila Wasala Avatar answered Oct 06 '22 05:10

Akila Wasala


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)                         }                 }             }         } 
like image 36
JAHelia Avatar answered Oct 06 '22 07:10

JAHelia