Search code examples
swiftuiswiftui-listios15xcode13

Unable to change iOS 15 SwiftUI List Section header padding


We are using SwiftUI and custom view for List Section's header.

But when compiling with Xcode13/iOS15 SDK, there seems to be extra left/right 20px + top/bottom 6px padding fixed in the header container view. I even created the bare minimum testing app, it seems unable to be customized.

This is not related to the newly introduced sectionHeaderTopPadding, so setting it to 0 doesn't work for me. I also tried .environment(\.defaultMinListHeaderHeight, 16) from this post, it also doesn't change the padding.

Here is snippet and screenshot:

        List {
            Section(header:
                Text("Big header")
                        .foregroundColor(.red)
                        .background(Color.gray)
                        .frame(height: 30)
                        .padding(0)
            ) {
                Text("Hello, world! 1")
                    .padding()
                Text("Hello, world! 2")
                    .padding()
                Text("Hello, world! 3")
                    .padding()
            }
        }
        .environment(\.defaultMinListHeaderHeight, 1)
        .listStyle(PlainListStyle())

enter image description here enter image description here

Thanks a lot for helping :) cheers


Solution

  • Finally found the answer myself.

    In iOS15, to remove padding for section header, you need to use .listRowInsets(EdgeInsets()) just like list cell.