Search code examples
swiftuiswiftui-listswiftui-navigationview

swiftui list with custom header


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?


Solution

  • 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