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
相关产品推荐
相关产品推荐

