我想点击一个按钮,并在SwiftUI中显示macOS应用程序的上下文菜单。我可以看到按钮,但当我点击按钮时,什么也不会发生。
let menuItems = ContextMenu {
Button("Today") {}
Button("Tomorrow") {}
}
Button {
// action
} label: {
Label("Add Date", systemImage: "calendar")
.contextMenu(menuItems)
}
有什么想法吗?
发布于 2021-11-25 09:30:24
在我看来,你只是在寻找按钮式菜单,就像
Menu {
Button("Today") {}
Button("Tomorrow") {}
} label: {
Label("Add Date", systemImage: "calendar")
}
.menuStyle(.borderedButton)
发布于 2021-11-25 08:57:40
在上下文菜单周围放置一个Button
意味着按钮正在管理所有的单击事件,而这些事件中没有一个通过contextMenu
修饰符。
您可以移动修饰符,将其附加到按钮本身,然后在单击左键时执行按钮操作,但在右击时显示上下文菜单:
Button {
print("Clicked")
} label: {
Label("Add Date", systemImage: "calendar")
}
.contextMenu(menuItems)
https://stackoverflow.com/questions/70114190
复制相似问题