1

I have a List with fixed heights. I removed all the insets using listRowInsets.

However, a 22 point padding still remains above the section header (shrinking to zero as scrolling starts). Is there a way to remove the extra padding above the section header?

UPDATE: The extra padding only appears in iOS 15 runtimes.

enter image description here

enter image description here

The spacing goes away if I remove the section header (leaving the rows in the ForEach only).

enter image description here

import SwiftUI
import Introspect

struct ContentView: View {
        
    var body: some View {
        ZStack {
            Grid().opacity(0.1)
            VStack {
                Spacer(minLength: 60)
                List {
                    Section(
                        header: HStack {
                            Text("Section Heaeder").font(.system(size: 40))
                            Spacer()
                        }
                            .frame(height: 60)
                            .background(Grid().opacity(0.4))
                            .listRowInsets(.zero),
                        content: {
                            ForEach(1...20, id: \.self) { eachRowIndex in
                                Text("Row \(eachRowIndex)")
                                    .frame(height: 40)
                                    .listRowInsets(.zero)
                                    .listRowBackground(
                                        Rectangle()
                                            .strokeBorder(Color.white.opacity(0.2), lineWidth: 1)
                                    )
                            }
                        }
                    )
                }
                .listStyle(.plain)
                .introspectTableView {
                    $0.separatorStyle = .none
                }
                .background(Grid().opacity(0.1))
                .padding(.leading, 20)
                .padding(.trailing, 25)
                .environment(\.defaultMinListRowHeight, 40)
            }
        }
        .edgesIgnoringSafeArea(.all)
    }
}

extension EdgeInsets {
    
    static var zero = EdgeInsets()
}

struct Grid: View {
    
    var body: some View {
        Rectangle()
            .fill(
                ImagePaint(image: Image("Square"))
            )
    }
}
Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
  • any update for this on ios 16 ? since ios 16 not using TableView anymore – Ryan_Chau Aug 25 '22 at 04:41
  • @Ryan_Chau Is this still an issue on iOS 16 runtimes at all in the first place? Regarding introspection: Apple seemingly switched to `UICollectionView`. See [this thread](https://twitter.com/JoshHrach/status/1533990021753348096). Either wait for [Introspect](https://github.com/siteline/SwiftUI-Introspect) update, or create your own introspect modifier according [Implement your own selector](https://github.com/siteline/SwiftUI-Introspect#implement-your-own-selector). – Geri Borbás Aug 25 '22 at 08:51
  • yup, it still happens in ios 16. Even with custom introspect, I can retrieve the collectionView, however, doesn't seem straightforward to modify section header top padding in collectionView. The only way I found is to assign a new layout – Ryan_Chau Aug 26 '22 at 00:13
  • @Ryan_Chau Can't you simply use [`sectionHeaderTopPadding`](https://developer.apple.com/documentation/uikit/uitableview/3750914-sectionheadertoppadding)? It is available since iOS 15. – Geri Borbás Aug 28 '22 at 17:57
  • I posted a question for [this](https://stackoverflow.com/questions/73518704/swiftui-plain-list-how-to-remove-top-header-padding-in-ios16) No, you can't, sectionHeaderTopPadding is for UITableView only – Ryan_Chau Aug 28 '22 at 23:50
  • @Ryan_Chau [`sectionInset`](https://developer.apple.com/documentation/uikit/uicollectionviewflowlayout/1617714-sectioninset) may help you out (depending on the underlying layout, though). – Geri Borbás Aug 30 '22 at 10:08

1 Answers1

1

Use sectionHeaderTopPadding. It targets this specific spacing.

The API is only available from iOS 15, but the extra spacing itself is present in iOS 15 runtimes only.

struct ContentView: View {
        
    init() {
        if #available(iOS 15.0, *) {
            UITableView.appearance().sectionHeaderTopPadding = 0
        }
    }

    ...
}

enter image description here

Geri Borbás
  • 15,810
  • 18
  • 109
  • 172