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

Is there a way to pin headers without Lazy(ness)?

Forums > SwiftUI

I've got the following basic setup for a View, which is aestetically pleasing for the information I want to present, but I can't figure out how to (a) prevent each "List" segment of the screen from being draggable/scrollable or (b) pin the headers so that scrolling the List doesn't scroll the Section header with it.

    var body: some View {
        VStack {
            List {
                Section {
                    Text("Header")
                } header: {
                    Text("Region 1")
                }

                Section {
                    Text("Body")
                } header: {
                    Text("Region 2")
                }
            }

            HStack {
                List {
                    Section {
                        Text("Image 1")
                    } header: {
                        Text("Region 3")
                    }
                }

                List {
                    Section {
                        Text("Image 2")
                    } header: {
                        Text("Region 4")
                    }
                }
            }

            List {
                Section {
                    ForEach(0..<7) { number in
                        HStack {
                            Spacer()
                            Text("Word \(number)")
                        }
                    }
                } header: {
                    Text("Region 5")
                }
            }
        }
    }

The Apple documentation seems to indicate that headers can only be pinned using LazyVStack and LazyHStack, which don't work for me here (when I prepend Lazy onto any of the Stacks in my code, that area of the screen goes blank).

Is there an alternate way to pin headers? And does anyone have advice on what I can use besides Lists and Sections to create the same, organized setup I have without the scrollability (as I assume that all List Views are going to do that).

2      

Add .listStyle(.plain) or .listStyle(.inset) to your List. I'm not sure there is a way to have sticky headers when using .insetGrouped.

2      

Thanks for the assist! The .inset works to keep the headers in place, though it removes the visual that I was looking for. So I'll just create my own View that looks like a Section-in-a-List using some rectangles/overlays/clipshapes.

Does anyone else roll their eyes at the explanations given for options on things like ListStyle? Per the developer documentation:

static var inset: InsetListStyle The list style that describes the behavior and appearance of an inset list."

That's 🙄 soooo helpful when I'm trying to figure out how things like .sidebar/.elliptical/.bordered look...

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.