SwiftUI: append menu entry to group "View" after "Enter Full Screen"

Viewed 198

I'd like to append a new menu entry right below "Enter Full Screen", but I am failing to find the right CommandGroupPlacement property.

CommandGroup(after: .<what needs to be put here??>) {
   //my buttons here
}

Attempting to override "View" results in just another group with the same name (see image).

enter image description here

CommandMenu("View") {
     //add button here       
}
2 Answers

Shoutout to Majid Jabrayilov for his blogpost on this: https://swiftwithmajid.com/2020/11/24/commands-in-swiftui/

To find a solution to my above issue though I still had to think a bit around the corner–what does work is this:

CommandGroup(before: .toolbar) {
                Button("Foo") {
                    
                }
            }

This works, because the toolbar menu entry is located within "View" (even thought I don't have a toolbar in my app, the placement still works nonetheless...)

You said you were looking for the proper CommandGroupPlacement so for your case (relative to full screen mode), you would technically want CommandGroupPlacement.sidebar. CommandGroupPlacement.toolbar works because toolbar options live in the View menu, though you have none set.

/// Example in a CommandsBuilder
CommandGroup(after: CommandGroupPlacement.sidebar) {
    Button("New View Action", action: { print("After sidebar menu options") }).keyboardShortcut("s", modifiers: [.command, .option, .control, .function]
}

This should also come in handy for others. The Apple documentation that corresponds to this: CommandGroupPlacement

Related