9

I want to set a image header for list in swiftui. The effect I want is shown in the figure below:

enter image description here

However, I can not remove padding in this image row. My code is as bellow:

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                Section() {
                    Image("HeadImage")
                        .resizable()
                        .frame(height: 150)
                }
                ForEach((0..<4), id: \.self) { index in
                    Section {
                        NavigationLink(destination: Text("aaa")) {
                            Label("Buttons", systemImage: "capsule")
                        }
                        NavigationLink(destination: Text("aaa")) {
                            Label("Colors", systemImage: "paintpalette")
                        }
                        NavigationLink(destination: Text("aaa")) {
                            Label("Controls", systemImage: "slider.horizontal.3")
                        }
                    }
                }
            }
            .navigationBarTitle("SwiftUI")
            .navigationBarTitleDisplayMode(.inline)
        }
        .accentColor(.accentColor)
    }
}

But the image row has a padding as bellow:

enter image description here

Is there any method to remove this padding?

1
  • Have you tried setting the aspect to .fill? or on the Section{} add .edgesIgnoringSafeArea() Commented Jul 21, 2022 at 14:48

1 Answer 1

25

It is row insets, they can be turned off as below

demo

Section() {
    Image("HeadImage")
        .resizable()
        .frame(height: 150)
        .listRowInsets(EdgeInsets())    // << here !!
}

Tested with Xcode 14 / iOS 16

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

5 Comments

That's it! You are super man!
I add a new question. Can you spare time to help me have a look?
@mars that is not how SO works, if you have a follow up question you should make a new post.
I tried, but it can only post a question every 90 minute... I will post it later
@asperi Here is the new question stackoverflow.com/questions/73074069/…

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.