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

iOS 18.2中SwiftUI如何让Section Header与列表项对齐?

解决iOS 18.2 SwiftUI Section Header与列表项对齐问题

在iOS 18.2中,使用.headerProminence(.increased)会给Section Header添加额外默认内边距,这就是导致它和列表项无法对齐的核心原因。以下是几个可行的解决方法:

方法1:调整Section Header全局内边距

通过.listSectionHeaderPadding修饰符直接抵消默认的leading方向内边距:

List {
    Section {
        Text("Row")
        Text("Row")
        Text("Row")
    } header: {
        Text("Header")
    }
    .headerProminence(.increased)
    .listSectionHeaderPadding(.init(top: 0, leading: -16, bottom: 0, trailing: 0))
}

这里的-16对应列表项默认的leading内边距,多数场景下这个值能让两者完美对齐。

方法2:给Header文本添加负padding

直接给Header的Text组件设置负的leading padding,精准调整位置:

List {
    Section {
        Text("Row")
        Text("Row")
        Text("Row")
    } header: {
        Text("Header")
            .padding(.leading, -16)
    }
    .headerProminence(.increased)
}

方法3:移除headerProminence修饰符(如果不需要增强样式)

如果不需要Section Header的增强突出样式,直接去掉.headerProminence(.increased),此时Header会默认和列表项对齐:

List {
    Section {
        Text("Row")
        Text("Row")
        Text("Row")
    } header: {
        Text("Header")
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 04:54:59