You need to enable JavaScript to run this app. SwiftUI开发中如何移除List组件的默认白色背景
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI开发中如何移除List组件的默认白色背景

当前UI

解决方法

方案1:适配iOS 16及以上版本

直接在List上添加.scrollContentBackground(.hidden)修饰符隐藏系统默认的列表滚动内容背景即可,原有listRowBackground的设置保留,修正后代码如下:

VStack(spacing: 30) {
    List(0 ..< 10) { item in
        Button(action: {
            print("stisa si za kafic")
        }, label: {
            CafeBarTab() 
        }).listRowBackground(Color.clear)
    }
    .scrollContentBackground(.hidden) // 隐藏系统默认列表背景
    .listStyle(.plain) // 可选,统一不同系统版本的列表样式
    .background(Color("background-Color"))
    .frame(width: 400, height: 5000, alignment: .leading)
}

方案2:兼容iOS 15及更早版本

额外在List的onAppear生命周期中清除UITableView的全局默认背景:

VStack(spacing: 30) {
    List(0 ..< 10) { item in
        Button(action: {
            print("stisa si za kafic")
        }, label: {
            CafeBarTab() 
        }).listRowBackground(Color.clear)
    }
    .listStyle(.plain)
    .onAppear {
        UITableView.appearance().backgroundColor = .clear
    }
    .background(Color("background-Color"))
    .frame(width: 400, height: 5000, alignment: .leading)
}

原因说明

你之前设置的List.background是作用在List外层容器上,SwiftUI的List内置了一层系统默认的白色滚动内容背景,会盖住外层自定义的背景色,必须先隐藏这层系统默认背景才能生效。之前设置的foregroundColor不需要和背景色一致,会影响列表内文本、控件的默认前景显示,建议删除。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.09 01:12:02