【问题标题】:SwiftUI context menu trigger by left click on macOS通过左键单击 macOS 触发 SwiftUI 上下文菜单
【发布时间】:2023-03-22 01:19:01
【问题描述】:

我的视图上有一个齿轮按钮,当用户单击它时,我想显示一个按钮菜单。 https://developer.apple.com/documentation/swiftui/view/contextmenu(menuitems:) 似乎是为此任务而设计的,但只有右键单击才能触发上下文菜单。

如何通过左键单击 macOS 来触发contextMenu?谢谢!

【问题讨论】:

  • 使用菜单代替 contextMenu。这是一个例子stackoverflow.com/a/63933231/12299030
  • @Asperi 感谢您的评论。我已经尝试过Menu,但菜单的点击区域是一个栏,我只想通过单击齿轮图标本身来触发菜单。

标签: macos swiftui


【解决方案1】:

您可以为该类型的 UI 使用特定的菜单样式。如果你想隐藏指标,只要输入false即可。

Menu("Label") {
      Button("Buttons") { }
}
.menuStyle(BorderlessButtonMenuStyle(showsMenuIndicator: true))
.fixedSize() // Otherwise will be the width of your menu options.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多