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

iOS15中如何将List默认灰色背景改为白色且不切换ForEach方案

解决方案

iOS 15对List的默认样式做了调整,默认使用insetGrouped分组样式自带灰色背景,不需要切换为纯ForEach实现,可通过以下方法解决:

最简通用方案(适配iOS 13+)

直接给List添加.listStyle(.plain)修饰符即可,Plain列表样式默认自带纯白色背景,无需额外配置:

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Test")
            List {
                ForEach(1..<20) { i in
                    Text(String(i))
                }         
            }
            .listStyle(.plain)
            .frame(maxWidth: .infinity)                
        }       
    }
}

保留inset分组边距方案

如果需要保留原有inset分组的行左右边距效果,不想切换为Plain列表的左右贴边样式,可根据适配版本选择方案:

适配iOS 16及以上
struct ContentView: View {
    var body: some View {
        VStack {
            Text("Test")
            List {
                ForEach(1..<20) { i in
                    Text(String(i))
                }         
            }
            .scrollContentBackground(.hidden) // 隐藏系统默认灰色背景
            .background(.white) // 设置为纯白色背景
            .frame(maxWidth: .infinity)                
        }       
    }
}

最终实现效果如下:
示例效果图

兼容iOS 15版本
  1. 在App入口的初始化方法中添加全局配置:
init() {
    UITableView.appearance().backgroundColor = .clear
}
  1. 给List添加白色背景即可:
List {
    ForEach(1..<20) { i in
        Text(String(i))
    }         
}
.background(.white)
.frame(maxWidth: .infinity)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 19:09:03