[英]SwiftUI List disclosure indicator without NavigationLink
我正在寻找一种解决方案来显示披露指示符 chevron 而无需将我的视图包装到NavigationLink
中。 例如,我想显示指示器但不导航到新视图,而是显示模式。
我找到了很多隐藏指示器按钮的解决方案,但没有一个可以解释如何添加一个。 在当前的 SwiftUI 版本中,这甚至可能吗?
struct MyList: View {
var body: some View {
NavigationView {
List {
Section {
Text("Item 1")
Text("Item 2")
Text("Item 3")
Text("Item 4")
}
}
}
}
例如,我想将披露指示符添加到Item 1
,而无需将其包装到NavigationLink
中
我已经尝试使用chevron.right
SF 符号来伪造指标,但该符号与默认的 iOS 符号 100% 不匹配。 顶部是默认底部是chevron.right
。
希望这是您正在寻找的。 您可以将项目添加到 HStack 并在之间使用 Spacer 假装它是一个链接:
HStack {
Text("Item 1")
Spacer()
Button(action: {
}){
Image(systemName: "chevron.right")
.font(.body)
}
}
这绝对是可能的。
您可以使用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() })
}
}
然后,在您的List
中,您可以对行执行以下操作:
// ...
ForEach(section.items) { item in
Button(action: {
// your custom navigation / action goes here
}) {
HStack {
Text(item.name)
Spacer()
NavigationLink.empty
}
}
}
// ...
以上产生的结果与您使用NavigationLink
相同,并且还按预期在交互中突出显示/取消突出显示该行。
在 iOS15 中,以下是更好的匹配,因为其他解决方案有点太大而且不够大胆。 与指定字体大小相比,它还可以更好地调整到不同的显示比例。
HStack {
Text("Label")
Spacer()
Image(systemName: "chevron.forward")
.font(Font.system(.caption).weight(.bold))
.foregroundColor(Color(UIColor.tertiaryLabel))
}
如果有一种官方的方式来做到这一点会很好。 更新每个操作系统调整很烦人。
或者也许创建一个假的并使用它,即使你点击你也可以调用你的事件。
NavigationLink(destination: EmptyView()) {
HStack {
Circle()
Text("TITLE")
}
}
.contentShape(Rectangle())
.onTapGesture {
print("ALERT MAYBE")
}
我找到了一个原始的解决方案。 手动插入图标不会带来完全相同的外观。
诀窍是使用带有“isActive”参数的初始化程序并传递一个始终为假的本地绑定。 所以 NavigationLink 等待一个永远不会发生的以编程方式触发的事件。
// use this initializer
NavigationLink(isActive: <Binding<Bool>>, destination: <() -> _>, label: <() -> _>)
您可以将空闭包传递给目标参数。 无论如何,它永远不会被调用。 要执行一些操作,您可以在 ZStack 的顶部放置一个按钮。
func navigationLinkStyle() -> some View {
let never = Binding<Bool> { false } set: { _ in }
return ZStack {
NavigationLink(isActive: never, destination: { }) {
Text("Item 1") // your list cell view
}
Button {
// do your action on tap gesture
} label: {
EmptyView() // invisible placeholder
}
}
}
对于可访问性,您可能需要模仿UIKit
版本的披露指示器。 您本身不需要以这种方式实现它,但如果您使用例如 Appium 进行测试,您可能希望这样来保持测试成功
显然UIKit
的披露指示器是一个禁用按钮,具有一些可访问性值,所以这里是解决方案:
struct DisclosureIndicator: View {
var body: some View {
Button {
} label: {
Image(systemName: "chevron.right")
.font(.body)
.foregroundColor(Color(UIColor.tertiaryLabel))
}
.disabled(true)
.accessibilityLabel(Text("chevron"))
.accessibilityIdentifier("chevron")
.accessibilityHidden(true)
}
}
已经提交的答案并没有说明一件事:点击时单元格的突出显示。 请参阅我的答案底部图像中的About Peek-a-View
单元格 - 它被突出显示,因为我在截取屏幕截图时按下了它。
我的解决方案同时考虑了这一点和雪佛龙:
Button(action: { /* handle the tap here */ }) {
NavigationLink("Cell title", destination: EmptyView())
}
.foregroundColor(Color(uiColor: .label))
当单元被点击时, Button
的存在似乎会通知 SwiftUI; 仅仅添加一个onTapGesture()
是不够的。
这种方法的唯一缺点是需要指定.foregroundColor()
; 没有它,按钮文本将改为蓝色。
我创建了一个自定义NavigationLink
:
action
API (而不是必须推送一个View
)List
单元格选择保持原样MYNavigationLink(action: {
didSelectCell()
}) {
MYCellView()
}
import SwiftUI
struct MYNavigationLink<Label: View>: View {
@Environment(\.colorScheme) var colorScheme
private let action: () -> Void
private let label: () -> Label
init(action: @escaping () -> Void, @ViewBuilder label: @escaping () -> Label) {
self.action = action
self.label = label
}
var body: some View {
Button(action: action) {
HStack(spacing: 0) {
label()
Spacer()
NavigationLink.empty
.layoutPriority(-1) // prioritize `label`
}
}
// Fix the `tint` color that `Button` adds
.tint(colorScheme == .dark ? .white : .black) // TODO: Change this for your app
}
}
// Inspiration:
// - https://stackoverflow.com/a/66891173/826435
private extension NavigationLink where Label == EmptyView, Destination == EmptyView {
static var empty: NavigationLink {
self.init(destination: EmptyView(), label: { EmptyView() })
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.