Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI List is not showing any items

I want to use NavigationView together with the ScrollView, but I am not seeing List items.

struct ContentView: View {
    var body: some View {
        NavigationView {
            ScrollView{
                VStack {
                    Text("Some stuff 1")
                    List{
                        Text("one").padding()
                        Text("two").padding()
                        Text("three").padding()
                    }
                    Text("Some stuff 2")
                }
            }
        }
    }
}

All I see is the text. If I remove ScrollView I see it all, but the text is being pushed to the very bottom. I simply want to be able to add List and Views in a nice scrollable page.

like image 426
Winten Avatar asked Sep 05 '25 03:09

Winten


2 Answers

The ScrollView expects dimension from content, but List expects dimension from container - as you see there is conflict, so size for list is undefined, and a result rendering engine just drop it to avoid disambiguty.

The solution is to define some size to List, depending of your needs, so ScrollView would now how to lay out it, so scroll view could scroll entire content and list could scroll internal content.

Eg.

demo

struct ContentView: View {
    @Environment(\.defaultMinListRowHeight) var minRowHeight

    var body: some View {
        NavigationView {
            ScrollView{
                VStack {
                    Text("Some stuff 1")
                    List {
                        Text("one").padding()
                        Text("two").padding()
                        Text("three").padding()
                    }.frame(minHeight: minRowHeight * 3).border(Color.red)
                    Text("Some stuff 2")
                }
            }
        }
    }
}
like image 149
Asperi Avatar answered Sep 07 '25 19:09

Asperi


Just wanted to throw out an answer that fixed what I was seeing very similar to the original problem - I had put a Label() item ahead of my List{ ... } section, and when I deleted that Label() { } I was able to see my List content again. Possibly List is buggy with other items surrounding it (Xcode 13 Beta 5).

like image 32
Kendall Helmstetter Gelner Avatar answered Sep 07 '25 20:09

Kendall Helmstetter Gelner