You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS16下SwiftUI Plain List如何移除分区头部内边距?

移除SwiftUI Plain Style List分区头部上方内边距(iOS16适配)

在iOS15中,我们可以通过UITableView.appearance().sectionHeaderTopPadding = 0移除SwiftUI List分区头部的上方内边距,但iOS16起List底层基于UICollectionView重写,原方法不再生效。

以下是示例代码:

import SwiftUI

struct TaskRow: View {
    var body: some View {
        Text("Task data goes here")
    }
}

struct HeaderText: View {
    var text:String
    var body: some View {
        Text(text)
            .font(.system(.title3))
            .fontWeight(.bold)
            .foregroundColor(.primary)
    }
}

struct ListWithSections: View {
    init() {
        if #available(iOS 15.0, *) {
            UITableView.appearance().sectionHeaderTopPadding = 0
        } else {
            // 适配更早版本
        }
    }
    
    var body: some View {
        if #available(iOS 16.0, *) {
            List {
                Section(header: HeaderText(text: "Section 1")) {
                    TaskRow()
                    TaskRow()
                    TaskRow()
                }
                
                Section(header: HeaderText(text: "Section 2")) {
                    TaskRow()
                    TaskRow()
                    TaskRow()
                }
                
            }
            .scrollContentBackground(.hidden)
            .background(Color.gray)
            .listStyle(.plain)
        } else {
            List {
                Section(header: HeaderText(text: "Section 1")) {
                    TaskRow()
                    TaskRow()
                    TaskRow()
                }
                
                Section(header: HeaderText(text: "Section 2")) {
                    TaskRow()
                    TaskRow()
                    TaskRow()
                }
                
            }
            .background(Color.gray)
            .listStyle(.plain)
        }
    }
}

效果对比

iOS15与iOS16默认效果差异

iOS15与iOS16默认效果对比

尝试方案及问题

使用listRowInset可以实现近乎一致的静态UI,但滚动时存在细微差异:

iOS15滚动效果(期望达成的目标)

iOS15滚动效果

iOS16使用listRowInset后的滚动效果

iOS16滚动效果

请问在Plain Style的List下,如何在iOS16中实现和iOS15完全一致的无分区头部上方内边距效果?

内容的提问来源于stack exchange,提问作者Ryan_Chau

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 07:48:45