0

I am trying to create a simple list view in SwiftUI however, it shows up as blank (pictured below) no matter how many objects are in the array. If I get rid of the list, it will show the elements of the array. I know for a fact that the array is not empty. Any recommendations on how to fix it.

This is my code"

var body: some View {
        NavigationView {
            ScrollView {
                if(leaderboard.isEmpty) {
                    
                    VStack {
                        Text("No Entries Yet :(")
                            .foregroundColor(.gray)
                            .font(.largeTitle)
                    }
                } else {
                    ScrollView {
                        List {
                            ForEach(leaderboard.asArray(), id: \.self) { score in
                                Text("\(score)")
                            }
                        }
                    }
                }
            }
            .navigationBarTitle("Leaderboard")
        }
    }

Here is what the view currently shows: Picture of SwiftUI preview where the screen where list should be is blank

Also, bonus points if you can help me make it so the Text("No Entries Yet :( ") is centered vertically within the view.

2
  • 2
    You don't need a ScrollView if you are using a List Commented Sep 10, 2021 at 2:41
  • Yeah and IIRC the ScrollView becomes extremely tiny in height to the point where you can't see it. Remove both ScrollViews. Commented Sep 10, 2021 at 2:47

2 Answers 2

1

You don't need either of those ScrollViews - If the list is empty you only have the text view. If the list isn't empty, List provides the necessary scroll view.

To vertically centre the text when the list is empty, simply add Spacer views above and below:

struct ContentView: View {
    var body: some View {
        
        let leaderboard:[Int] = [10,20,30,40,100,1999,1393,444]
        NavigationView {
            if(leaderboard.isEmpty) {
                VStack {
                    Spacer()
                    Text("No Entries Yet :(")
                        .foregroundColor(.gray)
                        .font(.largeTitle)
                    Spacer()
                }
            } else {
                List {
                    ForEach(leaderboard, id: \.self) { score in
                        Text("\(score)")
                    }
                }
            }
        }
        .navigationBarTitle("Leaderboard")
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

If your list is empty, just add another row without background. Stylish it as you want.

if rows.isEmpty {
    Text("No records")
        .listRowBackground(EmptyView())
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.