SwiftUI menu with failing trailing closure (MenuStyleConfiguration)

Viewed 2779

I am creating an iOS application with SwiftUI

I used the master/detail template and got to this:

struct ContentView: View {

var body: some View {
  
     NavigationView {
        
        if (conditionsForMasterDetail)
        {
          masterView
            .navigationBarTitle(Text("Master"))
            .navigationBarItems(
                leading: EditButton(),
                trailing: Button(
                    action: {
                        withAnimation { self.dates.insert(Date(), at: 0) }
                    }
                ) {
                    Image(systemName: "plus")
                }
            )
        }
        
        if (conditionsForDetaiView)
        {
          detailView
        }
        } //NavigationView
    .navigationViewStyle(DoubleColumnNavigationViewStyle()).toolbar {
        ToolbarItem(placement: .primaryAction) {
            Menu
            { //error here: “Trailing closure passed to parameter of type 'MenuStyleConfiguration' that does not accept a closure”                        
                    NavigationLink(destination:HelpView(appData: appData))
                                        {                        Button(action: {}) {
                        Label(help_menu_item, systemImage: "")
                    }
                    }
                    Button(action: {}) {
                        Label(liability_disclaimer_menu_item, systemImage: "")
                    }
                
                                       Button(action: {}) {
                        Label(R.string.about_menu_item, systemImage: "")
                    }
                    

                
                
            }//menu
             
                 label: {//error here: Extra argument 'label' in call
        Label("Menu", systemImage: "")
    }
         
    }//toolbar item
         
           
}//toolbar

} //body
} //ContentView

The problem is the menu, because an error is issued:

Trailing closure passed to parameter of type 'MenuStyleConfiguration' that does not accept a closure

Note that the trailing closure is just a different way to write parameters of an initializers. Menu seems to accept a configuration but I see the content and the label (error on label: Extra argument 'label' in call).

I tried all kind of writing the init without the trailing closure syntax but it does not lead to working code.

I would like to know if it is a bug or it is a real error.

2 Answers

This doesn't specifically answer this question but because I couldn't find any other answers to this topic.

Throws the same error:

struct TextPopup: View {
    var body: some View {
        Menu {
            Text("hello")
        }
    }
}

Compiles correctly:

struct TextPopup: View {
    var body: some View {
        Menu {
            Text("hello")
        } label: {}
    }
}

Here is somehow cleaned replicated your snapshot that compiled well with Xcode 12 / iOS 14. You can inject your specifics back one by one.

Note: Button in NavigationLink does not work and so has no sense (because NavigationLink is a button itself)

struct ContentView: View {
    @State private var conditionsForMasterDetail = false
    @State private var conditionsForDetaiView = false

    var body: some View {
        NavigationView {
            if (conditionsForMasterDetail)
            {
                Text("masterView")
                    .navigationBarTitle(Text("Master"))
                    .navigationBarItems(
                        leading: EditButton(),
                        trailing: Button(
                            action: {
                                withAnimation { /*self.dates.insert(Date(), at: 0)*/ }
                            }
                        ) {
                            Image(systemName: "plus")
                        }
                    )
            }

            if (conditionsForDetaiView)
            {
                Text("detailView")
            }
        } //NavigationView
        .navigationViewStyle(DoubleColumnNavigationViewStyle()).toolbar {
            ToolbarItem(placement: .primaryAction) {
                Menu
                {
                    NavigationLink(destination:Text("HelpView(appData: appData)"))
                    { Text("Link")
                        //                        Label(help_menu_item, systemImage: "")
                    }
                    Button(action: {}) {
                        Text("A")
                        //                        Label(liability_disclaimer_menu_item, systemImage: "")
                    }

                    Button(action: {}) {
                        Text("B")
                        //                        Label(R.string.about_menu_item, systemImage: "")
                    }
                }//menu

                label: {//error here: Extra argument 'label' in call
                    Label("Menu", systemImage: "")
                }
            }//toolbar item
        }//toolbar
    } //body
} //ContentView
Related