【问题标题】:SwiftUI: Background color of List with SidebarListStyle?SwiftUI:带有 SidebarListStyle 的列表的背景颜色?
【发布时间】:2021-07-21 15:46:16
【问题描述】:

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

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

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

但是,这实际上什么也没做。我的List 看起来完全一样。我该如何设置这个List的背景颜色?

【问题讨论】:

    标签: ios swift macos swiftui


    【解决方案1】:

    您可以使用此修饰符 .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)
        }
    }
    



    【讨论】:

    • 这适用于行本身。整个List的背景呢?它仍然显示为灰色。
    • @Shadowman 你可以使用UITableView.appearance().backgroundColor 作为列表的背景颜色我为此更新了我的帖子:)
    • 太棒了!不能使用 UIKit 的 macOS 应用程序是否有等效项?
    • 另外,UITableView.appearance() 方法会改变 every 列表背景颜色。有没有办法单独更改它们?
    • @Shadowman stackoverflow.com/questions/60454752/… 将是您第一次询问有关在 macOS 中使用的答案。对于第二个问题,您可以通过分别为每个列表创建一个结构来解决它。我不确定,如果现在没有 init(){UITableView.appearance()} 在 iOS 中使用 SwiftUI 列表背景颜色的方法
    猜你喜欢
    • 2019-11-29
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    相关资源
    最近更新 更多