繁体   English   中英

SwiftUI:带有 SidebarListStyle 的列表的背景颜色?

[英]SwiftUI: Background color of List with SidebarListStyle?

我有一个 SwiftUI List ,它的listStyle设置为SidebarListStyle 这给了我一个看起来像这样的List

在此处输入图片说明

伟大的! 但是,我想为其设置不同的背景颜色,但似乎不知道该怎么做。 我的代码如下所示:

List {
   <snip>
}
.listStyle(SidebarListStyle())
.background(Color.yellow.ignoresSafeArea())

但是,这实际上没有任何作用。 我的List看起来完全一样。 我该如何设置这个List的背景颜色?

您可以将此修饰符.listRowBackground()用于单独行的背景颜色。


struct ContentView: View {
    
    private var items = ["1", "2", "3"]
    
    init(){
        UITableView.appearance().backgroundColor = .purple // background color of list
    }
    
    var body: some View {
        List {
            ForEach(items, id: \.self) { item in
                Text(item)
                    .listRowBackground(Color.yellow) // background color of listRow
                Text(item)
                    .listRowBackground(Color.blue) // background color of listRow
            }
        }
        .frame(width: 300, height: 600)
    }
}



在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM