LAST CHANCE: Save 50% on all my Swift books and bundles! >>

SOLVED Day 42: Moonshot Challenge - List Layout Won't Display

Forums > 100 Days of SwiftUI

I created a list layout to go along with my grid layout, and the list looks just fine in the SwiftUI preview but when I toggle from grid to list it just shows a blank screen. :(

Here's my ContentView


import SwiftUI    

struct ContentView: View {

    let astronauts: [String: Astronaut] = Bundle.main.decode("astronauts.json")
    let missions: [Mission] = Bundle.main.decode("missions.json")

    @State var showingGrid = false

    var body: some View {
        NavigationView {
            ScrollView{
                Group {
                    if showingGrid {
                        GridLayout(astronauts: astronauts, missions: missions)
                    } else {
                        ListLayout(astronauts: astronauts, missions: missions)
                    }
                }
                .toolbar{
                    ToolbarItem() {
                        Button("Switch Layout") {
                            showingGrid.toggle()
                        }
                    }
                }
                .padding([.horizontal, .bottom])
            }
            .navigationTitle("Moonshot")
            .background(.darkBackground)
            .preferredColorScheme(.dark)

        }
    }
}

#Preview {
    ContentView()
}

And here's my ListLayout

import SwiftUI

struct ListLayout: View {

    let astronauts: [String: Astronaut]
    let missions: [Mission]

    var body: some View {
        List{
            ForEach(missions) { mission in
                NavigationLink {
                    MissionView(mission: mission, astronauts: astronauts)
                } label: {
                    HStack {
                        Image(mission.image)
                            .resizable()
                            .scaledToFit()
                            .frame(width: 100, height: 100)
                            .padding()

                        VStack {
                            Text(mission.displayName)
                                .font(.headline)
                                .foregroundColor(.white)
                            Text(mission.formattedLaunchDate)
                                .font(.caption)
                                .foregroundColor(.white.opacity(0.5))
                        }
                        .padding(.vertical)
                        .frame(maxWidth: .infinity)
                        .background(.lightBackground)
                    }
                    .clipShape(RoundedRectangle(cornerRadius: 10))
                    .overlay(RoundedRectangle(cornerRadius: 10)
                            .stroke(.lightBackground)
                             )
                }
            }
        }
    }
}

struct ListLayout_Preview: PreviewProvider {
    static let astronauts: [String: Astronaut] = Bundle.main.decode("astronauts.json")
    static let missions: [Mission] = Bundle.main.decode("missions.json")
    static var previews: some View {
        ListLayout(astronauts: astronauts, missions: missions)
            .preferredColorScheme(.dark)
    }
}

Thanks!

3      

My mistake here was keeping the ScrollView in ContentView. I moved that over to GridView and it worked!

3      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.