没有 NavigationLink 的 SwiftUI 列表披露指示器

gra*_*han 3 swift swiftui swiftui-list swiftui-navigationlink

我正在寻找一种解决方案来显示披露指示器 V 形,而无需将我的视图包装到NavigationLink. 例如,我想显示指示器但不想导航到新视图,而是显示一个模态。

我找到了很多隐藏指示器按钮的解决方案,但没有一个解决方案解释了如何添加一个。在当前的 SwiftUI 版本中这甚至可能吗?

struct MyList: View {
    var body: some View {
        NavigationView {
        List {
            Section {
                Text("Item 1")
                Text("Item 2")
                Text("Item 3")
                Text("Item 4")

            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

例如,我想将披露指示符添加到Item 1而无需将其包装成NavigationLink

我已经尝试使用chevron.rightSF 符号来伪造指标,但该符号与默认的 iOS 符号不 100% 匹配。顶部是默认底部是chevron.right

披露按钮图像

Geo*_*dis 18

这绝对是可能的。

Button您可以使用和 非功能性的组合NavigationLink来实现您想要的。

在 上添加以下扩展名NavigationLink

extension NavigationLink where Label == EmptyView, Destination == EmptyView {

   /// Useful in cases where a `NavigationLink` is needed but there should not be
   /// a destination. e.g. for programmatic navigation.
   static var empty: NavigationLink {
       self.init(destination: EmptyView(), label: { EmptyView() })
   }
}
Run Code Online (Sandbox Code Playgroud)

然后,在您的 中List,您可以对该行执行以下操作:

// ...
ForEach(section.items) { item in
    Button(action: {
        // your custom navigation / action goes here
    }) {
        HStack {
            Text(item.name)
            Spacer()
            NavigationLink.empty
        }
    }
 }
 // ...
Run Code Online (Sandbox Code Playgroud)

上面的结果与使用 a 的结果相同,NavigationLink并且还按照交互时的预期突出显示/取消突出显示该行。

  • 添加按钮修饰符“.buttonStyle(.plain)”将删除蓝色色调并更正显示 V 形的颜色。 (2认同)

小智 16

已提交的答案无法解释一件事:点击单元格时突出显示单元格。请参阅About Peek-a-View我的答案 \xe2\x80\x94 底部图像中的单元格,它被突出显示,因为我在截图时按下了它。

\n

我的解决方案同时考虑了这一点V 形:

\n
Button(action: { /* handle the tap here */ }) {\n    NavigationLink("Cell title", destination: EmptyView())\n}\n.foregroundColor(Color(uiColor: .label))\n
Run Code Online (Sandbox Code Playgroud)\n

Button当单元格被点击时,的存在似乎会通知 SwiftUI;仅仅添加一个onTapGesture()是不够的。

\n

这种方法的唯一缺点是.foregroundColor()需要指定;如果没有它,按钮文本将为蓝色。

\n

SwiftUI 列表的屏幕截图,其中一个单元格突出显示

\n


Gea*_*ten 8

希望这就是您正在寻找的。您可以将项目添加到 HStack 并在其间使用 Spacer 将其伪装为链接:

HStack {
                    Text("Item 1")
                    Spacer()
                    Button(action: {

                    }){
                        Image(systemName: "chevron.right")
                            .font(.body)
                    }
                }
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用颜色 `.foregroundColor(Color(UIColor.tertiaryLabel))` 那么它也可以正确支持深色模式。 (4认同)
  • 您可以尝试将字体大量编辑为正确的样式。我通过执行以下操作非常接近: .font(Font.system(size: 13, Weight: .semibold, design: .default)) .foregroundColor(Color(red: 0.771, green: 0.771, blue: 0.779)) (2认同)

ngb*_*ngb 7

在iOS15中,以下是一个更好的匹配,因为其他解决方案有点太大而且不够大胆。与指定字体大小相比,它还可以更好地调整大小以适应不同的显示比例。

HStack {
    Text("Label")
    Spacer()
    Image(systemName: "chevron.forward")
      .font(Font.system(.caption).weight(.bold))
      .foregroundColor(Color(UIColor.tertiaryLabel))
}
Run Code Online (Sandbox Code Playgroud)

如果有一个官方的方法来做到这一点就好了。更新每个操作系统调整是很烦人的。

  • 字体似乎更接近`.font(.footnote.bold())` (2认同)