SwiftUI - 如何将列表背景设置为透明?

3
在SwiftUI中,似乎有两个选项可以修改列表的颜色。
  1. 设置列表的行背景颜色

    List {        
      ForEach(elements, id:\.self) { element in
      }.listRowBackground(Color.blue)
    }
    
  2. 将列表的颜色方案设置为“浅色”或“深色”

     List {        
       ForEach(elements, id:\.self) { element in
       }
     }.colorScheme(.light) // .dark
    

设置其他属性,如背景、前景颜色等似乎对感兴趣的视图颜色没有任何影响。

目前,我将列表嵌入到导航视图中,如下所示:

NavigationView {
  List {        
    ForEach(elements, id:\.self) { element in
    }
  }.colorScheme(.light) // .dark
}

但是,再次强调,没有颜色设置或视图层次结构设置似乎能够使背景视图设置为清晰/透明,以便主视图的颜色可以起到驱动作用。相反,通过实验发现我们被迫选择白色或黑色背景。这是Swift或Xcode的错误还是存在解决方案?

enter image description here


1
这个回答解决了你的问题吗?https://dev59.com/ClEG5IYBdhLWcg3wI06g#72650158 - Asperi
@Asperi 虽然那里没有被接受的答案,但它确实引导了我走向正确的方向。 - lifewithelliott
1个回答

10
为了设置列表视图的背景颜色,必须将scrollContentBackground属性设置为隐藏,并将background属性设置为颜色。
List {
  ForEach(elements, id:\.self) { element in 
  }.listRowBackground(Color.white)
}.scrollContentBackground(.hidden)
 .background(Color.blue)

enter image description here


1
我已经寻找这个答案好几天了。谢谢。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接