3

I’m trying to recreate a layout similar to the Apple Health app. Specifically, I want the first element in a scrollable view to span edge-to-edge, while the rest of the content should look like a default (.insetGrouped) SwiftUI List.

Is there a way to achieve this effect within a List — having the first row ignore horizontal insets — or is this typically done using a ScrollView + VStack with custom styling to mimic List behavior?

I’d prefer to avoid fully recreating list sections manually if there’s a cleaner way to do this using List.

Apple Health app

2

1 Answer 1

1
List {
    Section {
    } header: {
        BasicHealthRangeCharts(samples: viewModel.healthData.compactMap(ChartHealthSample.init(from:)))
            .padding(.vertical)
    }
    .background(Color(UIColor.systemBackground).frame(width: 1000))
    .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))

    
    Section {
        Button {
            viewModel.onPinButton()
        } label: {
            Text(viewModel.isPinned ? "Unpin from summary" : "Pin in summary")
                .foregroundColor(.primary)
                .padding()
        }
        
    } header: {
        Text("Options")
            .padding(.vertical, 16)
    }
    .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 16))
    .headerProminence(.increased)
    
    Section {
        NavigationLink {
            PetHealthAllDataView(dataType: viewModel.type, data: viewModel.healthData)
        } label: {
            Text("Show all data")
        }
    }
}

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

You might want to consider setting the .leading and .trailing insets to 0, instead of making them negative. This way, the content will align with the list group and you don't have to guess the horizontal margin size. Then add the background color (white) as .background to the header, instead of as a ZStack layer. To extend the background to the screen edges, add negative horizontal padding to the color. This can be larger than actually required, for example, -100.
Great tips. I will update code later with your suggestions.
Edited code in answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.