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版本
- 在App入口的初始化方法中添加全局配置:
init() { UITableView.appearance().backgroundColor = .clear }
- 给List添加白色背景即可:
List { ForEach(1..<20) { i in Text(String(i)) } } .background(.white) .frame(maxWidth: .infinity)
内容的提问来源于stack exchange,提问作者user9582784
相关产品推荐
相关产品推荐

