【发布时间】:2020-12-29 13:06:49
【问题描述】:
我在 iOS14.3 (Xcode12.3) 上有一个 SwiftUI List,在它的单元格内,我希望在用户点击按钮时弹出一个 Menu。此外,这些单元格有一个onTapGesture,问题是按下菜单按钮时也会触发此手势。
示例:
List {
HStack {
Text("Cell content")
Spacer()
// Triggers also onTapGesture of cell
Menu(content: {
Button(action: { }) {
Text("Menu Item 1")
Image(systemName: "command")
}
Button(action: { }) {
Text("Menu Item 2")
Image(systemName: "option")
}
Button(action: { }) {
Text("Menu Item 3")
Image(systemName: "shift")
}
}) {
Image(systemName: "ellipsis")
.imageScale(.large)
.padding()
}
}
.background(Color(.systemBackground))
.onTapGesture {
print("Cell tapped")
}
}
如果您点击省略号图像,菜单会打开,但"Cell tapped" 也会打印到控制台。这对我来说是个问题,因为在我的真实示例中,我在点击手势中折叠单元格,当然我不希望在用户按下菜单按钮时发生这种情况。
我发现,当我长按按钮时,手势不会被触发。但在我看来,这不是可接受的用户体验,我花了一段时间才自己找出来。
我还注意到,当我将Menu 替换为常规Button 时,按下时不会触发单元格手势(仅使用BorderlessButtonStyle 或PlainButtonStyle,否则他根本不活动)。但我不知道如何从它的操作中打开菜单。
List {
HStack {
Text("Cell content")
Spacer()
// Does not trigger cells onTapGesture, but how to open Menu from action?
Button(action: { print("Button tapped") }) {
Image(systemName: "ellipsis")
.imageScale(.large)
.padding()
}
.buttonStyle(BorderlessButtonStyle())
}
.background(Color(.systemBackground))
.onTapGesture {
print("Cell tapped")
}
}
【问题讨论】:
-
是 iOS 还是 macOS 项目?
-
抱歉,它在 iOS14.3 和 Xcode 12.3 上
-
我现在也在 macOS 上对其进行了测试,打开菜单时不会触发单元格手势。尽管 Mac 上的菜单看起来非常不同(实际上是 DropDown),但我希望两个平台上的行为相同。所以我刚刚向 Apple 报告了一个错误。
标签: swiftui swiftui-list