UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.