0

SwiftUI DocumentGroup apps provide standard menu items including Help.

By default, when we run the app, and click Help / "MyApp help" we see "Help isn't available for MyApp".

Here's the main app:


import SwiftUI

@main
struct MyApp: App {

    var body: some Scene {

        DocumentGroup(newDocument: { MyDocument() }) { configuration in
            ContentView()
        }
        .commands {

            // use CommandGroup to modify built-in menu behavior
            // the following would work to replace the entire help menu
            // but I just want to access the logic for the 
            // second of two Help sub-menus

            CommandGroup(replacing: .help) {

                // How do I access the logic for Help / MyApp Help?


            }
        }
    }
}

I've reviewed the standard documentation, tutorials, and UI guidelines, but don't see much on how to integrate behaviors and adjustments to the standard menu options. Any assistance would be much appreciated.

This question from a year ago is similar, but unanswered.

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
denise
  • 116
  • 8
  • 1
    Add a button inside the command group closure. There is a WWDC video on this. Search it. – user1046037 Dec 05 '22 at 00:35
  • Thanks - I learned how to publish the app documentation from a video. Still trying to figure out how to get access to the menu correctly so I can replace just the the help / MyApp help option. Any suggestions on how to tap into sub menu items? – denise Dec 11 '22 at 12:11
  • 1
    Watch https://developer.apple.com/wwdc21/10062 Time: 13:13. Every time you want to implement a feature try to learn the concept. Start with WWDC video then framework documentation. Learning will take much longer than implementing it but you will benefit in the long run as you wouldn't run into odd corner cases – user1046037 Dec 11 '22 at 17:29
  • 1
    Replacing an existing existing menu - Use https://developer.apple.com/documentation/swiftui/commandgroup/init(replacing:addition:) – user1046037 Dec 11 '22 at 17:32
  • 1
    So many videos - your recommendation helps narrow it down. Thanks for both the link to menu replacement - and the video / reference project above - very helpful for us new SwiftUI developers. – denise Dec 17 '22 at 22:07

1 Answers1

1

When working with SwiftUI menus, the key objects are CommandGroup and CommandGroupPlacement.

There's a great example in "Swift with Majid: Commands in SwiftUI dated 24 Nov 2020". Scroll nearly to the end to see how to use CommandGroupPlacement (e.g., .help or .newItem) to identify locations on the standard menu.

The example shows how to indicate whether our content should appear before, or after, or whether it should be replacing the CommandGroupPlacement menu location specified.

I generally prefer written documentation over videos, but the video and example code recommended in the question comments is quite useful - thank you user1046037.

denise
  • 116
  • 8