BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

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

Forums > SwiftUI

Hi All, I'm quite new to swift and am wondering why I am receiving a "Trailing closure passed to parameter of type 'Visibility' that does not accept a closure" error on the .toolbar { line I Have Supplied My Code Below Thanks in Advance, Josh

import SwiftUI

struct HomeView: View {

    var body: some View {

        Text("Home")

            .toolbar {

                ToolbarItem(placement: .navigationBarLeading)

                Button(action: NavigationLink(destination: AccountView()), label: Image(systemName: "gearshape"))

                ToolbarItem(placement: .principal) { Text("Welcome").font(.title)

                }

                ToolbarItem(placement: .navigationBarTrailing)

                Button(action: NavigationLink(destination: AccountView()), label: Image(systemName: "person.circle"))

            }

            .navigationBarTitleDisplayMode(.inline)

    }

}

struct HomeView_Previews: PreviewProvider {

    static var previews: some View {

        HomeView()

    }

}

   

You are missing the { from ToolbarItem Also you do not need Button with NavigationLink

var body: some View {
        NavigationStack {
            Text("Home")
                .toolbar {
                    ToolbarItem(placement: .navigationBarLeading) {
                        NavigationLink {
                            AccountView()
                        } label: {
                            Image(systemName: "gearshape")
                        }

                    }
                }
        }
    }

   

In Xcode you can either click or double click on the curly brackets { } to see their counterpart.

Clicking will light up the counterpart, Double clicking will mark the whole block. This might be helpful to spot these kind of errors.

   

Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.