SwiftUI中List遮挡背景色问题:如何让背景显示在文本左侧?
解决SwiftUI List遮挡底层背景的问题
你的问题核心在于List本身默认带有不透明的系统背景,哪怕你尝试设置listRowBackground,List容器的背景还是会挡住底层的深色背景。另外你把listRowBackground加在了List本身而非单个列表项上,这也是它没生效的关键原因。
下面给你两个可行的解决思路,可根据目标iOS版本选择:
方案一:iOS 16+ 推荐用法
利用scrollContentBackground(.hidden)直接隐藏List的默认滚动内容背景,让底层颜色自然透出来:
struct ScrollSheetView: View { @State var upperBound = 0 @State var valueRange = Array(stride(from: 1, to: 100, by: 1)) var body: some View { ZStack { Color.mainBackground .ignoresSafeArea() VStack { List(valueRange, id: \.self) { index in Text("hello") .listRowBackground(Color.mainBackground) // 给每个列表行设置背景 } .listStyle(PlainListStyle()) .frame(maxWidth: .infinity) .scrollContentBackground(.hidden) // 隐藏List的默认背景层 } } } }
方案二:兼容iOS 15及更早版本
如果需要适配旧系统,用.background(.clear)将List的背景设为透明,同样能让底层颜色显示:
struct ScrollSheetView: View { @State var upperBound = 0 @State var valueRange = Array(stride(from: 1, to: 100, by: 1)) var body: some View { ZStack { Color.mainBackground .ignoresSafeArea() VStack { List(valueRange, id: \.self) { index in Text("hello") .listRowBackground(Color.mainBackground) } .listStyle(PlainListStyle()) .frame(maxWidth: .infinity) .background(.clear) // 设置List容器背景透明 } } } }
修改后,List会正常遵循安全区域布局,底层的深色背景会完全显示,包括列表项左侧的区域。
内容的提问来源于stack exchange,提问作者G26Bandit
相关产品推荐
相关产品推荐

