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 {
                Group {
                    if showingGrid {
                        GridLayout(astronauts: astronauts, missions: missions)
                    } else {
                        ListLayout(astronauts: astronauts, missions: missions)
                    ToolbarItem() {
                        Button("Switch Layout") {
                .padding([.horizontal, .bottom])


#Preview {

And here's my ListLayout

import SwiftUI

struct ListLayout: View {

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

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

                        VStack {
                        .frame(maxWidth: .infinity)
                    .clipShape(RoundedRectangle(cornerRadius: 10))
                    .overlay(RoundedRectangle(cornerRadius: 10)

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)



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


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.