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

iOS 17+Xcode15下SwiftUI List行分隔符不显示问题求助

iOS 17 + Xcode 15 下 SwiftUI List 行分隔符不显示问题解决

你遇到的是 iOS 17 中 SwiftUI List 渲染逻辑调整导致的兼容性问题,iOS 16 中 Section 上的行级修饰符可统一作用于内部所有行,但 iOS 17 里当 Section 同时包含自定义行与 ForEach 动态行时,Section 上的修饰符无法正确传递给后续的 ForEach 行。

修复方案

把原本加在 Section 上的行相关修饰符,直接移到 ForEach 的每个子项上,同时保留头部自定义行的独立样式设置:

ZStack {
    Color(.blue)
        .ignoresSafeArea()
    VStack {
        VStack(spacing: 10) {
            List {
                Section {
                    // 自定义头部行
                    VStack(alignment: .leading, spacing: 3) {
                        Text("Header")
                        Text("A small paragraph description")
                    }
                    .padding(EdgeInsets(top: 20, leading: 20, bottom: 28, trailing: 20))
                    .listRowBackground(Color(.pink))
                    .listRowSeparator(.hidden)
                    .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))

                    // ForEach 子项单独配置行样式
                    ForEach(1..<10) { value in
                        Text("\(value)")
                            .listRowBackground(Color(.green))
                            .listRowSeparatorTint(Color(.white))
                            .listRowSeparator(.visible)
                            .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
                    }
                }
                .listSectionSeparator(.hidden) // 仅保留Section级的分隔隐藏设置
            }
            .listStyle(PlainListStyle())
            .scrollContentBackground(.hidden)
            Spacer()
        }
    }
}
.navigationTitle("")
.navigationBarTitleDisplayMode(.inline)

原因说明

iOS 17 对 List 的 Section 渲染逻辑做了优化:当 Section 内存在多种不同类型的行(自定义视图行 + 动态生成行)时,Section 上的行级修饰符会被第一个自定义行的样式覆盖,导致后续 ForEach 的行无法继承这些配置。将修饰符直接绑定到每个 ForEach 子项上,能确保每一行都正确应用预期样式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 05:46:23