TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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()

    }

}

1      

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")
                        }

                    }
                }
        }
    }

1      

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.

1      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.