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

SwiftUI(iOS16):如何移除两个Section之间的间距?

移除SwiftUI List中Section之间的间距

方案1:iOS 16+ 原生SwiftUI实现

直接给List添加.listSectionSpacing(0)修饰符,这是最简洁的原生方式,无需依赖UIKit:

NavigationStack {
    List {
        Section {
            NavigationLink(value: Color.mint) {
                HStack {
                    Image(systemName: "person")
                        .font(.system(size: 25))
                        .padding(.trailing)
                    Text("Hi hello")
                        .font(.anuRegular(16))
                }
                .frame(minHeight: 45)
            }
        }
        Section {
            NavigationLink(value: Color.mint) {
                HStack {
                    Image(systemName: "person")
                        .font(.system(size: 25))
                        .padding(.trailing)
                    Text("Hi hello")
                        .font(.anuRegular(16))
                }
                .frame(minHeight: 45)
            }
        }
    }
    .listSectionSpacing(0) // 核心设置:将Section间距设为0
    .navigationDestination(for: Color.self) { color in
        
    }
    .navigationTitle("Categories")
}

方案2:兼容iOS 15及以下版本

如果需要适配更低系统版本,仅设置sectionFooterHeight不够——两个Section之间的间距由上一个Section的footer高度和下一个Section的header高度共同构成,需同时将两者设为0,还可按需移除顶部内边距:

struct YourView: View {
    init() {
        UITableView.appearance().sectionHeaderHeight = 0
        UITableView.appearance().sectionFooterHeight = 0
        // 若需要完全消除Section顶部的额外空隙,可添加这行
        UITableView.appearance().sectionHeaderTopPadding = 0
    }
    
    var body: some View {
        NavigationStack {
            List {
                Section {
                    NavigationLink(value: Color.mint) {
                        HStack {
                            Image(systemName: "person")
                                .font(.system(size: 25))
                                .padding(.trailing)
                            Text("Hi hello")
                                .font(.anuRegular(16))
                        }
                        .frame(minHeight: 45)
                    }
                }
                Section {
                    NavigationLink(value: Color.mint) {
                        HStack {
                            Image(systemName: "person")
                                .font(.system(size: 25))
                                .padding(.trailing)
                            Text("Hi hello")
                                .font(.anuRegular(16))
                        }
                        .frame(minHeight: 45)
                    }
                }
            }
            .navigationDestination(for: Color.self) { color in
                
            }
            .navigationTitle("Categories")
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 13:31:01