8

I have a gear button on my view, when user click it I want to show a menu of buttons. It seems the https://developer.apple.com/documentation/swiftui/view/contextmenu(menuitems:) is designed for this task but only right click can trigger the context menu.

How can I trigger the contextMenu by left-click on macOS? Thanks!

2
  • Use Menu instead of contextMenu. Here is an example stackoverflow.com/a/63933231/12299030. Commented Feb 22, 2021 at 16:01
  • 1
    @Asperi Thanks for your comment. I already tried the Menu but the hit area of the Menu is a bar and I want to trigger the menu only by clicking the gear icon itself. Commented Feb 23, 2021 at 6:18

1 Answer 1

23

You can use a specific menu style for that type of UI. If you want to hide the indicator, just put in false.

Menu("Label") {
      Button("Buttons") { }
}
.menuStyle(BorderlessButtonMenuStyle())
.menuIndicator(.hidden)
.fixedSize() // Otherwise will be the width of your menu options.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.