How to remove extra top padding in InsetGroupedListStyle List under iOS 15?
Asked Answered
P

3

11

I have an InsetGroupedListStyle List in SwiftUI and noticed an extra top padding added in iOS 15. How can I control or remove this?

List {
    Section(header: Text("Header")) {
        // some content
    }
}
.listStyle(InsetGroupedListStyle())

Here is iOS 14:

enter image description here

and iOS 15:

enter image description here

Phrasal answered 24/9, 2021 at 1:6 Comment(3)
This might help: #68155590Trine
This is useful but doesn't help in my case as the issue is not the section header top padding but actually the entire List top padding.Phrasal
I'm wondering if it could be related to new refresh controls added to list in iOS15Phrasal
R
7

For fixing this problem you can use the headerProminence

Section("Header") {
  // some content
}
.headerProminence(.increased)

iOS 15 vs 14

To fix the problem with table view

if #available(iOS 15.0, *)
    UITableView.appearance().sectionHeaderTopPadding = 0;

UPDATED: I think I found the solution for this specific case:

tableView.tableHeaderView = .init(frame: .init(x: 0, y: 0, width: 0, height: CGFloat.leastNonzeroMagnitude))
Raker answered 24/9, 2021 at 1:44 Comment(10)
This works for sections but doesn't help in my case as the issue is not the section header top padding but actually the entire List top padding. I tried this and made no difference.Phrasal
did you tried setting tableView.sectionHeaderTopPadding = 0.0 directly?Raker
what do you mean directly. I'm using swiftUI so no tableView. Just a list and no modifier for sectionHeader. That's why need to use UITableView.appearance()Phrasal
@Phrasal OK I understand you now, I've edited my answerRaker
This is not the solution. It's not to do with header prominence. This is the list adding a top padding.Phrasal
@Phrasal I've added an image to show you the effect of headerProminence, please just try it out, I think it will helpRaker
I have tried and makes no difference. I still have the extra space on iOS 15 which I don’t want. It’s only at top of list. Not every section.Phrasal
didn't work for me either. I am using SwiftUIPractise
I've updated my answer, please check out outRaker
.headerProminence(.increased) doesn't work for me...Snaky
T
6

I had a similarly frustrating issue, doing this helped me

List {
    Section(header: Text("Header")) {
        // some content
    }
}
.listStyle(PlainListStyle())

Note this changed the colours of my headers

Throne answered 28/9, 2021 at 14:54 Comment(0)
V
0

I'm late in the answering, but after spending almost a week to solve this issue I found a solution that solve my problem...

First, you need to add SwiftUIIntrospect to your project

List {
    Text("Item before section #0")
    ForEach(0..<10) { section in
        Section {
            ForEach(0..<6) { row in
                Text("Item #\(section)-#\(row)")
            }
        } header: {
            HStack {
                Text("Section #\(section)")
                    .foregroundColor(.black)
                Spacer()
            }
            .padding([.horizontal], 16)
            .frame(height: 40.0)
            .background(Color.cyan)
            .listRowInsets(.zero)
        }
    }
}
.listStyle(.plain)
.introspect(.list, on: .iOS(.v15)) { tableView in
    let identifier = "TableViewConfigured"
    guard tableView.focusGroupIdentifier != identifier else {
        return
    }
    tableView.focusGroupIdentifier = identifier

    tableView.sectionHeaderTopPadding = 0
}
.introspect(.list(style: .plain), on: .iOS(.v16, .v17)) { collectionView in
    let identifier = "CollectionViewConfigured"
    guard
        collectionView.focusGroupIdentifier != identifier,
        let collectionViewLayout = collectionView.collectionViewLayout as? UICollectionViewCompositionalLayout else {
        return
    }
    collectionView.focusGroupIdentifier = identifier

    let configuration = collectionViewLayout.configuration

    configuration.interSectionSpacing = -22

    collectionViewLayout.configuration = configuration
    collectionView.setCollectionViewLayout(collectionViewLayout, animated: false)
}

PS: On iOS 16 or 17, if the first element of your list is a section, it will still have the issue. In my case, it's not a problem because I have a cell without a section before the first section...


Result: enter image description here

Vaughn answered 28/4, 2024 at 21:46 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.