简体   繁体   English

如何在 SwiftUI 列表中设置所选行的背景颜色?

[英]How do I set the selected row background color in a SwiftUI List?

I am using a standard List in SwiftUI and would like to modify the selected rows' background color.我在 SwiftUI 中使用标准列表并想修改所选行的背景颜色。

Here is the code:这是代码:

...
List {
    ForEach(recipesToShow, id: \.self) { recipe in
        NavigationLink(destination: RecipeDetailWrapper(recipe: recipe)) {
            RecipeListRow(recipe: recipe)
                .environmentObject(recipeDateFormatter)
        }
    }
    .onDelete(perform: deleteRecipe)
} //: LIST
.listStyle(PlainListStyle())
...

This is how it looks by default:这是默认情况下的外观:

选定行背景颜色

you can use the modifier .listRowBackground(Color.blue) to modify the background color of your list.您可以使用修饰符 .listRowBackground(Color.blue) 来修改列表的背景颜色。

For the specific item, I would add a state var to store the id of the element tapped and then evaluate it in each item of the list,对于特定的项目,我会添加一个状态变量来存储被点击的元素的 id,然后在列表的每个项目中对其进行评估,

you can check the answer here:你可以在这里查看答案:

How do you change the select color of a navigation link in a list 如何更改列表中导航链接的选择颜色

that I think is what you need.我认为这就是你所需要的。

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

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