list - 修改行背景颜色列表 SwiftUI
问题描述
我正在尝试更改列表中的行颜色。
struct ContentView: View {
@State var userProfiles : [Profile] = [Profile.default]
var body: some View {
VStack{
List(userProfiles.indices, id: \.self, rowContent: row(for:)).background(Color.red)
}.background(Color.red)
}
// helper function to have possibility to generate & inject proxy binding
private func row(for idx: Int) -> some View {
let isOn = Binding(
get: {
// safe getter with bounds validation
idx < self.userProfiles.count ? self.userProfiles[idx] : DtrProfile.default
},
set: { self.userProfiles[idx] = $0 }
)
return Toggle(isOn: isOn.isFollower, label: { Text("\(idx)").background(Color.red) } )
}
}
输出:-
我想实现:-
有人可以向我解释如何更改整行颜色。我已经尝试按上面的方法实现,但还没有结果。
任何帮助将不胜感激。
提前致谢。
解决方案
利用listRowBackground
var body: some View {
VStack{
List {
ForEach(0...15, id: \.self, content: row(for:)).background(Color.red).listRowBackground(Color.red)
}
}
}
推荐阅读
- swift - 如何在 Xcode 中为命令行工具项目复制捆绑资源?
- ruby-on-rails - Rails:如何在自定义操作中添加 HTTP AUTH
- java - 如何修复“Volley:[15771] BasicNetwork.performRequest:android 中的意外响应代码 404?
- ruby-on-rails - new relic ruby vm 如何显示每个实例的线程
- asp.net-web-api - 如何解决 dotnet core web api 中的 Web API AmbiguousActionException?
- chart.js - ChartJS 仅对一个数据集进行动画处理
- automation - 如何在 cypress 中使用命令行无头运行 e2e 测试
- ios - 量角器 - 如何检查对象是否包含在另一个对象中?
- db2 - 将时间格式(hh:mm) 转换为十进制格式
- android - 当 enable=true 时 TextInputLayout 轮廓颜色的不同颜色然后是黑色并且启用 false 而不是浅灰色