GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

A view that combines a list and a grid in SwiftUI iOS

Forums > SwiftUI

Hello all,

I'm trying to create a view that contains a List and Grid in the same view - like, for example the Apple Music Library page which has a list at the top and a grid of album images below. I want the whole page to be scrollable.

Is this possible in SwiftUI? Everything I've tried breaks the layout and I can't find anything in my searches - Lists just seem to like to be the only item on the page (except in a ZStack but then only the list scrolls).

Thanks Russell

   

Combining a List and a Grid in the same scrollable view in SwiftUI can be challenging because Lists tend to dominate the layout. One effective approach is to use a ScrollView with a VStack to contain both elements, ensuring the entire page remains scrollable. Here’s a simple example:

import SwiftUI

struct ContentView: View { var body: some View { ScrollView { VStack(alignment: .leading, spacing: 20) { // List at the top VStack(alignment: .leading) { Text("List Header") .font(.headline) ForEach(0..<5) { index in Text("List item (index)") } } .padding()

            // Grid of album images below
            LazyVGrid(columns: [GridItem(.adaptive(minimum: 100))], spacing: 20) {
                ForEach(0..<20) { index in
                    Rectangle()
                        .fill(Color.blue)
                        .frame(height: 100)
                        .overlay(Text("Item \(index)"))
                }
            }
            .padding()
        }
    }
}

}

This code uses a ScrollView to ensure the entire content is scrollable, with a VStack inside to organize the List and Grid. You can customize the appearance and spacing as needed. This approach should help you achieve the desired layout without breaking the scroll functionality.

Hope this may help you !

1      

Thank you, Alex.

Really appreciate the detailed explanation and example. Will give it a go first thing tomorrow morning!

Cheers Russell

   

Hacking with Swift is sponsored by Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

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.