问题描述
我想在 MacOS 的 SwiftUI 中设置列表中一行的背景颜色。
原则上,使用修饰符 .listRowBackground
可以正常工作。
如何为选定行的(浅灰色)左边缘设置颜色(参见屏幕截图中的箭头)。
有趣的是,如果点击“一些文本”,所选行的左边缘是浅灰色的。
点击行中的空闲空间,左边缘为蓝色(见截图2)。
。
任何想法,差异来自哪里,更重要的是,我如何为所选行的这个“边缘”设置颜色?
这是我尝试过的:
struct SimpleListColorTest2: View {
@State var sel :Int? = 3
var body: some View {
List(selection: $sel) {
SimpleColoredLine2(lineNr:0,selection: $sel).tag(0)
SimpleColoredLine2(lineNr:1,selection: $sel).tag(1)
SimpleColoredLine2(lineNr:2,selection: $sel).tag(2)
SimpleColoredLine2(lineNr:3,selection: $sel).tag(3)
SimpleColoredLine2(lineNr:4,selection: $sel).tag(4)
SimpleColoredLine2(lineNr:5,selection: $sel).tag(5)
}
}
}
struct SimpleColoredLine2: View {
var lineNr : Int
@Binding var selection : Int?
var body: some View {
HStack {
Text("X2: ").background(Color.yellow)
Text("some Text").background(Color.yellow)
}
.onTapGesture(count: 1,perform: {
print("Line clicked")
selection = lineNr
})
.listRowBackground(selection == lineNr ? Color.red :Color.clear)
}
}
有什么想法吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)