微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 MacOS 应用程序的 Swift-UI 中为选定的列表行的背景着色

如何解决在 MacOS 应用程序的 Swift-UI 中为选定的列表行的背景着色

我想在 MacOS 的 SwiftUI 中设置列​​表中一行的背景颜色。
原则上,使用修饰符 .listRowBackground 可以正常工作。

如何为选定行的(浅灰色)左边缘设置颜色(参见屏幕截图中的箭头)。

enter image description here

有趣的是,如果点击“一些文本”,所选行的左边缘是浅灰色的。
点击行中的空闲空间,左边缘为蓝色(见截图2)。

enter image description here


任何想法,差异来自哪里,更重要的是,我如何为所选行的这个“边缘”设置颜色?

这是我尝试过的:

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 举报,一经查实,本站将立刻删除。