簡體   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